Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Acessar descrição de tabela relacionada Olá, Estou tendo dificuldades em acessar dados de tabela relacionada. Segue fonte abaixo. grid cliente ...
RB
Acessar descrição de tabela relacionada  
Olá,

Estou tendo dificuldades em acessar dados de tabela relacionada.

Segue fonte abaixo.

grid cliente
  1. <?php
  2. /**
  3.  * DatagridQuickView
  4.  */
  5. class DatagridCheckView extends TPage
  6. {
  7.       private $datagrid;
  8.       private $pageNavigation;
  9.       private $loaded;
  10.       private $form;
  11.       private $editar;
  12.       
  13.     
  14.     public function __construct()
  15.     {
  16.         parent::__construct();
  17.         
  18.         $this->form = new TQuickForm('form_cliente');        
  19.         $this->datagrid = new TDataGrid;
  20.         //$this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  21.         $this->datagrid->style="width: 100%"// Responsive 
  22.         $id                 = new TDataGridColumn('id','#','');
  23.         $nome               = new TDataGridColumn('pessoa->nome','Nome','');
  24.         $genero             = new TDataGridColumn('genero_id','Sexo','');
  25.         $data_nascimento    = new TDataGridColumn('pessoa->data_nascimento','Data Nascto','');
  26.         $civil              = new TDataGridColumn('civil->descricao','Estado Cívil','');
  27.         $etnia              = new TDataGridColumn('etnia->descricao','Etnia','');
  28.         $cpf                = new TDataGridColumn('cpf','CPF','');
  29.         $rg                 = new TDataGridColumn('rg','RG','');
  30.         $telefone           = new TDataGridColumn('pessoa->contato->telefone','Telefone','');
  31.         //$cep                = new TDataGridColumn('cep','Cep');
  32.         //$logradouro         = new TDataGridColumn('logradouro','Logradouro');
  33.         //$bairro             = new TDataGridColumn('bairro','Bairro');
  34.         //$cidade             = new TDataGridColumn('cidade','Cidade');
  35.         //$uf                 = new TDataGridColumn('uf','UF');
  36.         $email              = new TDataGridColumn('email','E-mail','');
  37.         $site               = new TDataGridColumn('site','Site','');
  38.         $organizacao        = new TDataGridColumn('organizacao_id','Empresa','');
  39.         $editar             = new TDataGridColumn('editar','','');
  40.         $deletar            = new TDataGridColumn('deletar','','');
  41.         $this->datagrid->addColumn($id);
  42.         $this->datagrid->addColumn($nome);
  43.         $this->datagrid->addColumn($genero);
  44.         $this->datagrid->addColumn($data_nascimento);
  45.         $this->datagrid->addColumn($civil);
  46.         $this->datagrid->addColumn($etnia);
  47.         $this->datagrid->addColumn($cpf);
  48.         $this->datagrid->addColumn($rg);
  49.         $this->datagrid->addColumn($telefone);
  50.         //$this->datagrid->addColumn($cep);
  51.         //$this->datagrid->addColumn($logradouro);
  52.         //$this->datagrid->addColumn($bairro);
  53.         //$this->datagrid->addColumn($cidade);
  54.         //$this->datagrid->addColumn($uf);
  55.         $this->datagrid->addColumn($email);
  56.         $this->datagrid->addColumn($site);
  57.         $this->datagrid->addColumn($organizacao);        
  58.         $this->datagrid->addColumn($editar);
  59.         $this->datagrid->addColumn($deletar); 
  60.         // creates the datagrid actions
  61.         $order1 = new TAction(array($this'onReload'));
  62.         $order2 = new TAction(array($this'onReload'));
  63.         // define the ordering parameters
  64.         $order1->setParameter('order''id');
  65.         $order2->setParameter('order''nome');
  66.         // assign the ordering actions
  67.         $id->setAction($order1);
  68.         $nome->setAction($order2);       
  69.         $btn_novo = new TButton('Novo');
  70.         $btn_novo->setAction(new TAction(array('ClienteFisicoBuilder','onClear')),'Novo');
  71.         $btn_novo->setImage('ico_edit.png');
  72.         $btn_novo->style="text-align: left;";
  73.         $this->form->addField($btn_novo);
  74.                 
  75.         // creates the datagrid model
  76.         $this->datagrid->createModel();
  77.         // creates the page navigation
  78.         $this->pageNavigation = new TPageNavigation;
  79.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  80.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  81.         
  82.         // wrap the page content using vertical box
  83.         $vbox = new TVBox;
  84.         $vbox->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  85.         $vbox->add($this->form);
  86.         $vbox->add($this->datagrid);
  87.         $vbox->add($btn_novo);
  88.         $vbox->add($this->pageNavigation);
  89.         parent::add($vbox);
  90.         
  91.     }
  92.     
  93.     /**
  94.      * Load the data into the datagrid
  95.      */
  96.     public function onReload($param NULL)
  97.     {
  98.         $this->datagrid->clear();
  99.         
  100.         try{
  101.              //abre a transação com a base
  102.              TTransaction::open('centinel');
  103.              //cria um repositório para carregar 'Cliente Fisico'
  104.              $cliente  = new TRepository('Fisica');
  105.              $limit 10;
  106.              //cria um critério para filtrar os dados conforme usuário logado
  107.              $criteria = new TCriteria;
  108.              // default order
  109.             if (empty($param['order']))
  110.             {
  111.                 $param['order'] = 'id';
  112.                 $param['direction'] = 'desc';
  113.             }
  114.             
  115.             $get_session TSession::getValue('organizacion_id');//pega id da empresa na seção do usuário
  116.              
  117.             $criteria->setProperties($param); // order, offset
  118.             $criteria->add(new TFilter('organizacao_id''='$get_session));
  119.             $criteria->setProperty('limit',$limit);
  120.              
  121.             $objects $cliente->load($criteriaFALSE);
  122.              if($objects)
  123.              {
  124.                  foreach($objects as $object)
  125.                  {
  126.                   //cria os botões de ação e adiciona a datagrid   
  127.                     $edit = new TImage('fa:edit blue');
  128.                     $action = new TAction(array('ClienteFisicoBuilder''onEdit'));
  129.                     $action->setParameter('key'$object->id);
  130.                     $a = new TActionLink($edit$action);                    
  131.                     $del = new TImage('fa:trash red');                    
  132.                     $object->deletar $del;
  133.                     $action1 = new TAction( [$this'onDelete' ] );
  134.                     $action1->setParameter('key'$object->id);
  135.                     $b = new TActionLink($del$action1);
  136.                     $object->editar $a
  137.                     $object->deletar $b
  138. /*
  139.                     $row->popover = 'true';
  140.                     $row->popside = 'top';
  141.                     $row->poptitle = 'Endereco(s)';
  142.                     $row->popcontent = "<table class='popover-table'>
  143.                     <tr bgcolor='red'>
  144.                         <td>Cep</td>
  145.                         <td>Logradouro</td>
  146.                         <td>Bairro</td>
  147.                     </tr>
  148.                     <tr>
  149.                         <td>{$object->cep}</td>
  150.                         <td>{$object->logradouro}</td>
  151.                         <td>{$object->bairro}</td>
  152.                     </tr>
  153.                     <tr>
  154.                         <td>{$object->cep2}</td>
  155.                         <td>{$object->logradouro2}</td>
  156.                         <td>{$object->bairro2}</td>
  157.                     </tr>
  158.                     <tr></tr>
  159.                     </table>";
  160.                     
  161.                     $object->row = $row;
  162.                     $row = $this->datagrid->addItem($object);
  163.                     */
  164.                     $this->datagrid->addItem($object);                    
  165.                  }
  166.              }
  167.              // reset the criteria for record count
  168.             $criteria->resetProperties();
  169.             $count $cliente->count($criteria);
  170.             
  171.             $this->pageNavigation->setCount($count); // count of records
  172.             $this->pageNavigation->setProperties($param); // order, page
  173.             $this->pageNavigation->setLimit($limit); // limit
  174.              
  175.             TTransaction::close();
  176.             $this->loaded TRUE;
  177.            }
  178.            catch (Exception $e)
  179.            {
  180.                  new TMessage('error',$e->getMessage());
  181.                  TTransaction::rollback();
  182.            }        
  183.     }
  184.     
  185.     /**
  186.      * shows the page
  187.      */
  188.     public function show(){
  189.         // check if the datagrid is already loaded
  190.         if (!$this->loaded){
  191.             $this->onReloadfunc_get_arg(0) );
  192.         }
  193.         parent::show();
  194.     }
  195.     function onDelete($param)
  196.     {
  197.         // define the next action
  198.         $action3 = new TAction(array($this'onInative'));
  199.         $action3->setParameters($param); // pass 'key' parameter ahead
  200.         
  201.         // shows a dialog to the user
  202.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'),$action3);
  203.     }
  204.     function onInative($param)
  205.     {
  206.         $organizacion_id TSession::getValue('organizacion_id');
  207.         $id $param['id'];
  208.         $cliente Cliente::inativaCliente($id,$organizacion_id);
  209.         if($cliente 1)
  210.         {
  211.         new TMessage('info',TAdiantiCoreTranslator::translate('Records deleted'));
  212.          $this->onReload();
  213.         //new TMessage('info',TAdiantiCoreTranslator::translate('Records deleted'), new TAction(array('DatagridCheckView','onReload')));
  214.         }
  215.     }
  216. }
  217. ?>




model Fisica
  1. <?php
  2. /**
  3.  * Fisica Active Record
  4.  * @author  <your-name-here>
  5.  */
  6. class Fisica extends TRecord
  7. {
  8.     const TABLENAME 'fisica';
  9.     const PRIMARYKEY'id';
  10.     const IDPOLICY =  'serial'// {max, serial}
  11.     
  12.     
  13.     private $pessoa;
  14.     private $civil;
  15.     private $etnia;
  16.     private $fisica;
  17.     /**
  18.      * Constructor method
  19.      */
  20.     public function __construct($id NULL$callObjectLoad TRUE)
  21.     {
  22.         parent::__construct($id$callObjectLoad);
  23.         parent::addAttribute('pessoa_id');
  24.         parent::addAttribute('cpf');
  25.         parent::addAttribute('rg');
  26.         parent::addAttribute('genero_id');
  27.         parent::addAttribute('civil_id');
  28.         parent::addAttribute('cnh');
  29.         parent::addAttribute('criacao');
  30.         parent::addAttribute('atualizacao');
  31.         parent::addAttribute('organizacao_id');
  32.         parent::addAttribute('etnia_id');
  33.     }
  34.     
  35.     /**
  36.      * Method set_pessoa
  37.      * Sample of usage: $fisica->pessoa = $object;
  38.      * @param $object Instance of Pessoa
  39.      */
  40.     public function set_pessoa(Pessoa $object)
  41.     {
  42.         $this->pessoa $object;
  43.         $this->pessoa_id $object->id;
  44.     }
  45.     
  46.     /**
  47.      * Method get_pessoa
  48.      * Sample of usage: $fisica->pessoa->attribute;
  49.      * @returns Pessoa instance
  50.      */
  51.     public function get_pessoa()
  52.     {
  53.         // loads the associated object
  54.         if (empty($this->pessoa))
  55.             $this->pessoa = new Pessoa($this->pessoa_id);
  56.     
  57.         // returns the associated object
  58.         return $this->pessoa;
  59.     }
  60.     
  61.     
  62.     /**
  63.      * Method set_civil
  64.      * Sample of usage: $fisica->civil = $object;
  65.      * @param $object Instance of Civil
  66.      */
  67.     public function set_civil(Civil $object)
  68.     {
  69.         $this->civil $object;
  70.         $this->civil_id $object->id;
  71.     }
  72.     
  73.     /**
  74.      * Method get_civil
  75.      * Sample of usage: $fisica->civil->attribute;
  76.      * @returns Civil instance
  77.      */
  78.     public function get_civil()
  79.     {
  80.         // loads the associated object
  81.         if (empty($this->civil))
  82.             $this->civil = new Civil($this->civil_id);
  83.     
  84.         // returns the associated object
  85.         return $this->civil;
  86.     }
  87.     
  88.     
  89.     /**
  90.      * Method set_etnia
  91.      * Sample of usage: $fisica->etnia = $object;
  92.      * @param $object Instance of Etnia
  93.      */
  94.     public function set_etnia(Etnia $object)
  95.     {
  96.         $this->etnia $object;
  97.         $this->etnia_id $object->id;
  98.     }
  99.     
  100.     /**
  101.      * Method get_etnia
  102.      * Sample of usage: $fisica->etnia->attribute;
  103.      * @returns Etnia instance
  104.      */
  105.     public function get_etnia()
  106.     {
  107.         // loads the associated object
  108.         if (empty($this->etnia))
  109.             $this->etnia = new Etnia($this->etnia_id);
  110.     
  111.         // returns the associated object
  112.         return $this->etnia;
  113.     }
  114.     
  115.     public function getContatos()
  116.     { 
  117.         try
  118.         {
  119.             TTransaction::open('centinel');
  120.         $repository = new TRepository('Contato');
  121.         $this->fisica $repository->where('pessoa_id''='$this->pessoa_id)->load();
  122.         return $this->fisica;
  123.         /*
  124.         if (empty($this->contato))
  125.         $this->contato = new Contato();
  126.         $this->contato->pessoa_id = $this->pessoa_id;
  127.         */
  128.         TTransaction::close();
  129.         }
  130.         catch (Exception $e)
  131.         {
  132.             new TMessage('error'$e->getMessage());
  133.         }
  134.         
  135.     }
  136. }
  137. model Pessoa
  1. <?php
  2. /**
  3.  * Pessoa Active Record
  4.  * @author  <your-name-here>
  5.  */
  6. class Pessoa extends TRecord
  7. {
  8.     const TABLENAME 'pessoa';
  9.     const PRIMARYKEY'id';
  10.     const IDPOLICY =  'serial'// {max, serial}
  11.     
  12.     
  13.     private $contato;
  14.     private $pessoa;
  15.     private $contatoweb;
  16.     /**
  17.      * Constructor method
  18.      */
  19.     public function __construct($id NULL$callObjectLoad TRUE)
  20.     {
  21.         parent::__construct($id$callObjectLoad);
  22.         parent::addAttribute('nome');
  23.         parent::addAttribute('data_nascimento');
  24.         parent::addAttribute('organizacao_id');
  25.         parent::addAttribute('criacao');
  26.         parent::addAttribute('atualizacao');
  27.         parent::addAttribute('ativo');
  28.         parent::addAttribute('tipo_pessoa_id');
  29.     }
  30.     
  31.     /**
  32.      * Method get_contato
  33.      * Sample of usage: $pessoa->contato->attribute;
  34.      * @returns Contato instance
  35.      */
  36.     public function get_contato()
  37.     {
  38.         // loads the associated object
  39.         if (empty($this->contato))
  40.             $this->contato = new Contato($this->pessoa->id);
  41.     
  42.         // returns the associated object
  43.         return $this->contato;
  44.     }
  45.     
  46.     public function getContatos()
  47.     { 
  48.         if (empty($this->contato))
  49.         $this->contato = new Contato($this->pessoa->id);
  50.         return $this->contato;
  51.     }
  52.     
  53.     
  54.     /**
  55.      * Method get_fisica
  56.      * Sample of usage: $pessoa->fisica->attribute;
  57.      * @returns Fisica instance
  58.      */
  59.     public function get_pessoa()
  60.     {
  61.         // loads the associated object
  62.         if (empty($this->pessoa))
  63.             $this->pessoa = new Fisica($this->pessoa->id);
  64.     
  65.         // returns the associated object
  66.         return $this->pessoa;
  67.     }
  68.         
  69.     /**
  70.      * Method get_contatoweb
  71.      * Sample of usage: $pessoa->contatoweb->attribute;
  72.      * @returns Contatoweb instance
  73.      */
  74.     public function get_contatoweb()
  75.     {
  76.         // loads the associated object
  77.         if (empty($this->contatoweb))
  78.             $this->contatoweb = new Contatoweb($this->pessoa->id);
  79.     
  80.         // returns the associated object
  81.         return $this->contatoweb;
  82.     }
  83. }
  84. model Contato
  1. <?php
  2. /**
  3.  * Contato Active Record
  4.  * @author  <your-name-here>
  5.  */
  6. class Contato extends TRecord
  7. {
  8.     const TABLENAME 'contato';
  9.     const PRIMARYKEY'id';
  10.     const IDPOLICY =  'serial'// {max, serial}
  11.     
  12.     
  13.     private $pessoa;
  14.     /**
  15.      * Constructor method
  16.      */
  17.     public function __construct($id NULL$callObjectLoad TRUE)
  18.     {
  19.         parent::__construct($id$callObjectLoad);
  20.         parent::addAttribute('pessoa_id');
  21.         parent::addAttribute('ddi');
  22.         parent::addAttribute('ddd');
  23.         parent::addAttribute('telefone');
  24.         parent::addAttribute('operadora_id');
  25.         parent::addAttribute('tipo_telefone_id');
  26.         parent::addAttribute('ramal');
  27.         parent::addAttribute('criacao');
  28.         parent::addAttribute('atualizacao');
  29.     }
  30.     
  31.     /**
  32.      * Method set_pessoa
  33.      * Sample of usage: $contato->pessoa = $object;
  34.      * @param $object Instance of Pessoa
  35.      */
  36.     public function set_pessoa(Pessoa $object)
  37.     {
  38.         $this->pessoa $object;
  39.         $this->pessoa_id $object->id;
  40.     }
  41.     
  42.     /**
  43.      * Method get_pessoa
  44.      * Sample of usage: $contato->pessoa->attribute;
  45.      * @returns Pessoa instance
  46.      */
  47.     public function get_pessoa()
  48.     {
  49.         // loads the associated object
  50.         if (empty($this->pessoa))
  51.             $this->pessoa = new Pessoa($this->pessoa_id);
  52.     
  53.         // returns the associated object
  54.         return $this->pessoa;
  55.     }
  56.     
  57.     public function get_fisica()
  58.     { 
  59.         if (empty($this->fisica))
  60.         $this->fisica = new Fisica($this->pessoa_id);
  61.         return $this->fisica;
  62.     }
  63.     
  64. }
  65. no aguardo da ajuda.
  66. Obrigado

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (2)


NR

Como uma pessoa pode ter mais do que um contato, o relacionamento entre eles não deve ser uma agregação?
RB

Nataniel, boa tarde.

Então estou usando apenas associação simples.

já consegui resolver, mais uma vez obrigado.