PHP code example of ajtarragona / censat-client

1. Go to this page and download the library: Download ajtarragona/censat-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ajtarragona / censat-client example snippets


use Censat;
...
public  function  test(){
    $censos=Censat::censuses();
    ...
}

'aliases'  =>  [
    ...
    'Censat'  =>  Ajtarragona\Censat\Facades\Censat::class
]

use Ajtarragona\Censat\Models\CensatClient;
...

public  function  test(CensatClient  $censat){
    $censos=$censat->censuses();
    ...
}

...
public  function  test(){
    $censos=censat()->censuses();
    ...
}

[
    ["id"=>  "name",  "value"=>"john",  "operation"=>"contains"],
    ["id"=>  "gender",  "value"=>"male"],  //default operation =
    ["id"=>  "age",  "value"=>18,  "operation"=>">"],
    ["id"=>  "active",  "operation"=>"isnotnull"],  //value not needed
    ["id"=>  "tags",  "operation"=>"in",  "value"=>[1,2,3]],
    ["concat"=>  "or",  "filters"=>[
        ["id"=>  "gender",  "value"=>"female"]
    ]]
]

    try{
        $instance= Censat::createInstance("census_name","entity_name",[
            "name" => "John",
            "surname" => "Smith",
            "age" => 25,
            "addresses_grid"=> [
                [
                    "street"=>"Fake street",
                    "number"=> 1
                ],
                [
                    "street"=>"Dumb street",
                    "number"=> 33
                    "floor" => 1
                ]
            ],
            "tags" => [1,2,4], //relation field
            'document_simple' => [
                "file-name"=>"doc_name.pdf",
                "file-content"=>$binary_content
            ],
            'document_multiple' => [
                [
                    "file-name"=>"doc_name1.pdf",
                    "file-content"=>$binary_content
                ],
                [
                    "file-name"=>"doc_name2.pdf",
                    "file-content"=>$binary_content
                ]
            ]
        ]);
    }catch(Exception $e){
        ...
    }

$entity=$census->entity('test')

$instances=$entity->forCensus("census_name")->all();

namespace App\Models;

use Ajtarragona\Censat\Models\Eloquent\CensatEntityModel;

class NomEntitat extends CensatEntityModel
{
    public $entity_name = 'nom_entitat';  // nom de la entitat
    public $census_id = 28; //id del cens (només necessari si la entitat està a més d'un cens)
    
}


class Tramit extends CensatEntityModel
{
    ...
    protected $dates = [
        'data_inici',
        'data_final'
    ];
    ...
    

class Tramit extends CensatEntityModel
{
    ...
    protected $casts = [
        'unitat_organica' => 'object'
    ];
    ...
    


class Tramit extends CensatEntityModel
{
 
    public $entity_name = 'tramit';
    
    
    protected $simple_relations = [
        'estruc_org' => '\App\Models\Tramits\UnitatOrganica'
    ];

    protected $multiple_relations = [
        'classificacio_tematica' => '\App\Models\Tramits\TematicaTramit',
        'classificacio_perfil' => '\App\Models\Tramits\Perfil'
    ];

    ...


$tramit=Tramit::find(1);
$tramit->estruc_org; //aixo retorna una instància de \App\Models\Tramits\UnitatOrganica o null
$tramit->classificacio_tematica; //aixo retorna una col·lecció
$tramit->classificacio_tematica()->where('id','>',10)->orderBy('id') //aqui tenim el QueryBuilder

$tramits=Tramit::has('classificacio_tematica')->get() //retorna tramits amb alguna classsificació temàtica



class Tramit extends CensatEntityModel
{
 
    public $entity_name = 'tramit';
    
    
    protected $simple_selects = [
        'tipus_instancia' => '\App\Models\Tramits\TipusSolicitud',
        'destinatari' => '\App\Models\Tramits\Destinatari'
    ];

          
    protected $multiple_selects = [
        'formes_tramitacio' => '\App\Models\Tramits\FormaTramitacio'
    ];

    ...


namespace App\Models\Tramits;
use Ajtarragona\Censat\Models\Eloquent\CensatSelectModel;

class TipusSolicitud extends CensatSelectModel
{
    public $entity_name="tramit";
    public $field_name="tipus_instancia";

}  


class Tramit extends CensatEntityModel
{
 
    public $entity_name = 'tramit';
    
    
    protected $grids = [
        'autors' => '\App\Models\Tramits\Autor'
    ];   
    
    ...


namespace App\Models\Tramits;
use Ajtarragona\Censat\Models\Eloquent\CensatGridModel;

class Autor extends CensatGridModel
{
    public $entity_name ="tramit";
    public $grid_name ="autors";

   ...
}    
bash
php artisan vendor:publish --tag=ajtarragona-censat-config