RB
Acessar descrição de tabela relacionada
Olá,
Estou tendo dificuldades em acessar dados de tabela relacionada.
Segue fonte abaixo.
grid cliente
model Fisica
Estou tendo dificuldades em acessar dados de tabela relacionada.
Segue fonte abaixo.
grid cliente
- <?php
- /**
- * DatagridQuickView
- */
- class DatagridCheckView extends TPage
- {
- private $datagrid;
- private $pageNavigation;
- private $loaded;
- private $form;
- private $editar;
- public function __construct()
- {
- parent::__construct();
- $this->form = new TQuickForm('form_cliente');
- $this->datagrid = new TDataGrid;
- //$this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->datagrid->style="width: 100%"; // Responsive
- $id = new TDataGridColumn('id','#','');
- $nome = new TDataGridColumn('pessoa->nome','Nome','');
- $genero = new TDataGridColumn('genero_id','Sexo','');
- $data_nascimento = new TDataGridColumn('pessoa->data_nascimento','Data Nascto','');
- $civil = new TDataGridColumn('civil->descricao','Estado Cívil','');
- $etnia = new TDataGridColumn('etnia->descricao','Etnia','');
- $cpf = new TDataGridColumn('cpf','CPF','');
- $rg = new TDataGridColumn('rg','RG','');
- $telefone = new TDataGridColumn('pessoa->contato->telefone','Telefone','');
- //$cep = new TDataGridColumn('cep','Cep');
- //$logradouro = new TDataGridColumn('logradouro','Logradouro');
- //$bairro = new TDataGridColumn('bairro','Bairro');
- //$cidade = new TDataGridColumn('cidade','Cidade');
- //$uf = new TDataGridColumn('uf','UF');
- $email = new TDataGridColumn('email','E-mail','');
- $site = new TDataGridColumn('site','Site','');
- $organizacao = new TDataGridColumn('organizacao_id','Empresa','');
- $editar = new TDataGridColumn('editar','','');
- $deletar = new TDataGridColumn('deletar','','');
- $this->datagrid->addColumn($id);
- $this->datagrid->addColumn($nome);
- $this->datagrid->addColumn($genero);
- $this->datagrid->addColumn($data_nascimento);
- $this->datagrid->addColumn($civil);
- $this->datagrid->addColumn($etnia);
- $this->datagrid->addColumn($cpf);
- $this->datagrid->addColumn($rg);
- $this->datagrid->addColumn($telefone);
- //$this->datagrid->addColumn($cep);
- //$this->datagrid->addColumn($logradouro);
- //$this->datagrid->addColumn($bairro);
- //$this->datagrid->addColumn($cidade);
- //$this->datagrid->addColumn($uf);
- $this->datagrid->addColumn($email);
- $this->datagrid->addColumn($site);
- $this->datagrid->addColumn($organizacao);
- $this->datagrid->addColumn($editar);
- $this->datagrid->addColumn($deletar);
- // creates the datagrid actions
- $order1 = new TAction(array($this, 'onReload'));
- $order2 = new TAction(array($this, 'onReload'));
- // define the ordering parameters
- $order1->setParameter('order', 'id');
- $order2->setParameter('order', 'nome');
- // assign the ordering actions
- $id->setAction($order1);
- $nome->setAction($order2);
- $btn_novo = new TButton('Novo');
- $btn_novo->setAction(new TAction(array('ClienteFisicoBuilder','onClear')),'Novo');
- $btn_novo->setImage('ico_edit.png');
- $btn_novo->style="text-align: left;";
- $this->form->addField($btn_novo);
- // creates the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- $vbox->add($this->datagrid);
- $vbox->add($btn_novo);
- $vbox->add($this->pageNavigation);
- parent::add($vbox);
- }
- /**
- * Load the data into the datagrid
- */
- public function onReload($param = NULL)
- {
- $this->datagrid->clear();
- try{
- //abre a transação com a base
- TTransaction::open('centinel');
- //cria um repositório para carregar 'Cliente Fisico'
- $cliente = new TRepository('Fisica');
- $limit = 10;
- //cria um critério para filtrar os dados conforme usuário logado
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'desc';
- }
- $get_session = TSession::getValue('organizacion_id');//pega id da empresa na seção do usuário
- $criteria->setProperties($param); // order, offset
- $criteria->add(new TFilter('organizacao_id', '=', $get_session));
- $criteria->setProperty('limit',$limit);
- $objects = $cliente->load($criteria, FALSE);
- if($objects)
- {
- foreach($objects as $object)
- {
- //cria os botões de ação e adiciona a datagrid
- $edit = new TImage('fa:edit blue');
- $action = new TAction(array('ClienteFisicoBuilder', 'onEdit'));
- $action->setParameter('key', $object->id);
- $a = new TActionLink($edit, $action);
- $del = new TImage('fa:trash red');
- $object->deletar = $del;
- $action1 = new TAction( [$this, 'onDelete' ] );
- $action1->setParameter('key', $object->id);
- $b = new TActionLink($del, $action1);
- $object->editar = $a;
- $object->deletar = $b;
- /*
- $row->popover = 'true';
- $row->popside = 'top';
- $row->poptitle = 'Endereco(s)';
- $row->popcontent = "<table class='popover-table'>
- <tr bgcolor='red'>
- <td>Cep</td>
- <td>Logradouro</td>
- <td>Bairro</td>
- </tr>
- <tr>
- <td>{$object->cep}</td>
- <td>{$object->logradouro}</td>
- <td>{$object->bairro}</td>
- </tr>
- <tr>
- <td>{$object->cep2}</td>
- <td>{$object->logradouro2}</td>
- <td>{$object->bairro2}</td>
- </tr>
- <tr></tr>
- </table>";
- $object->row = $row;
- $row = $this->datagrid->addItem($object);
- */
- $this->datagrid->addItem($object);
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count = $cliente->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- TTransaction::close();
- $this->loaded = TRUE;
- }
- catch (Exception $e)
- {
- new TMessage('error',$e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * shows the page
- */
- public function show(){
- // check if the datagrid is already loaded
- if (!$this->loaded){
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- function onDelete($param)
- {
- // define the next action
- $action3 = new TAction(array($this, 'onInative'));
- $action3->setParameters($param); // pass 'key' parameter ahead
- // shows a dialog to the user
- new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'),$action3);
- }
- function onInative($param)
- {
- $organizacion_id = TSession::getValue('organizacion_id');
- $id = $param['id'];
- $cliente = Cliente::inativaCliente($id,$organizacion_id);
- if($cliente = 1)
- {
- new TMessage('info',TAdiantiCoreTranslator::translate('Records deleted'));
- $this->onReload();
- //new TMessage('info',TAdiantiCoreTranslator::translate('Records deleted'), new TAction(array('DatagridCheckView','onReload')));
- }
- }
- }
- ?>
model Fisica
- <?php
- /**
- * Fisica Active Record
- * @author <your-name-here>
- */
- class Fisica extends TRecord
- {
- const TABLENAME = 'fisica';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'serial'; // {max, serial}
- private $pessoa;
- private $civil;
- private $etnia;
- private $fisica;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('pessoa_id');
- parent::addAttribute('cpf');
- parent::addAttribute('rg');
- parent::addAttribute('genero_id');
- parent::addAttribute('civil_id');
- parent::addAttribute('cnh');
- parent::addAttribute('criacao');
- parent::addAttribute('atualizacao');
- parent::addAttribute('organizacao_id');
- parent::addAttribute('etnia_id');
- }
- /**
- * Method set_pessoa
- * Sample of usage: $fisica->pessoa = $object;
- * @param $object Instance of Pessoa
- */
- public function set_pessoa(Pessoa $object)
- {
- $this->pessoa = $object;
- $this->pessoa_id = $object->id;
- }
- /**
- * Method get_pessoa
- * Sample of usage: $fisica->pessoa->attribute;
- * @returns Pessoa instance
- */
- public function get_pessoa()
- {
- // loads the associated object
- if (empty($this->pessoa))
- $this->pessoa = new Pessoa($this->pessoa_id);
- // returns the associated object
- return $this->pessoa;
- }
- /**
- * Method set_civil
- * Sample of usage: $fisica->civil = $object;
- * @param $object Instance of Civil
- */
- public function set_civil(Civil $object)
- {
- $this->civil = $object;
- $this->civil_id = $object->id;
- }
- /**
- * Method get_civil
- * Sample of usage: $fisica->civil->attribute;
- * @returns Civil instance
- */
- public function get_civil()
- {
- // loads the associated object
- if (empty($this->civil))
- $this->civil = new Civil($this->civil_id);
- // returns the associated object
- return $this->civil;
- }
- /**
- * Method set_etnia
- * Sample of usage: $fisica->etnia = $object;
- * @param $object Instance of Etnia
- */
- public function set_etnia(Etnia $object)
- {
- $this->etnia = $object;
- $this->etnia_id = $object->id;
- }
- /**
- * Method get_etnia
- * Sample of usage: $fisica->etnia->attribute;
- * @returns Etnia instance
- */
- public function get_etnia()
- {
- // loads the associated object
- if (empty($this->etnia))
- $this->etnia = new Etnia($this->etnia_id);
- // returns the associated object
- return $this->etnia;
- }
- public function getContatos()
- {
- try
- {
- TTransaction::open('centinel');
- $repository = new TRepository('Contato');
- $this->fisica = $repository->where('pessoa_id', '=', $this->pessoa_id)->load();
- return $this->fisica;
- /*
- if (empty($this->contato))
- $this->contato = new Contato();
- $this->contato->pessoa_id = $this->pessoa_id;
- */
- TTransaction::close();
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- }
- model Pessoa
- <?php
- /**
- * Pessoa Active Record
- * @author <your-name-here>
- */
- class Pessoa extends TRecord
- {
- const TABLENAME = 'pessoa';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'serial'; // {max, serial}
- private $contato;
- private $pessoa;
- private $contatoweb;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('nome');
- parent::addAttribute('data_nascimento');
- parent::addAttribute('organizacao_id');
- parent::addAttribute('criacao');
- parent::addAttribute('atualizacao');
- parent::addAttribute('ativo');
- parent::addAttribute('tipo_pessoa_id');
- }
- /**
- * Method get_contato
- * Sample of usage: $pessoa->contato->attribute;
- * @returns Contato instance
- */
- public function get_contato()
- {
- // loads the associated object
- if (empty($this->contato))
- $this->contato = new Contato($this->pessoa->id);
- // returns the associated object
- return $this->contato;
- }
- public function getContatos()
- {
- if (empty($this->contato))
- $this->contato = new Contato($this->pessoa->id);
- return $this->contato;
- }
- /**
- * Method get_fisica
- * Sample of usage: $pessoa->fisica->attribute;
- * @returns Fisica instance
- */
- public function get_pessoa()
- {
- // loads the associated object
- if (empty($this->pessoa))
- $this->pessoa = new Fisica($this->pessoa->id);
- // returns the associated object
- return $this->pessoa;
- }
- /**
- * Method get_contatoweb
- * Sample of usage: $pessoa->contatoweb->attribute;
- * @returns Contatoweb instance
- */
- public function get_contatoweb()
- {
- // loads the associated object
- if (empty($this->contatoweb))
- $this->contatoweb = new Contatoweb($this->pessoa->id);
- // returns the associated object
- return $this->contatoweb;
- }
- }
- model Contato
- <?php
- /**
- * Contato Active Record
- * @author <your-name-here>
- */
- class Contato extends TRecord
- {
- const TABLENAME = 'contato';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'serial'; // {max, serial}
- private $pessoa;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('pessoa_id');
- parent::addAttribute('ddi');
- parent::addAttribute('ddd');
- parent::addAttribute('telefone');
- parent::addAttribute('operadora_id');
- parent::addAttribute('tipo_telefone_id');
- parent::addAttribute('ramal');
- parent::addAttribute('criacao');
- parent::addAttribute('atualizacao');
- }
- /**
- * Method set_pessoa
- * Sample of usage: $contato->pessoa = $object;
- * @param $object Instance of Pessoa
- */
- public function set_pessoa(Pessoa $object)
- {
- $this->pessoa = $object;
- $this->pessoa_id = $object->id;
- }
- /**
- * Method get_pessoa
- * Sample of usage: $contato->pessoa->attribute;
- * @returns Pessoa instance
- */
- public function get_pessoa()
- {
- // loads the associated object
- if (empty($this->pessoa))
- $this->pessoa = new Pessoa($this->pessoa_id);
- // returns the associated object
- return $this->pessoa;
- }
- public function get_fisica()
- {
- if (empty($this->fisica))
- $this->fisica = new Fisica($this->pessoa_id);
- return $this->fisica;
- }
- }
- no aguardo da ajuda.
- Obrigado
Como uma pessoa pode ter mais do que um contato, o relacionamento entre eles não deve ser uma agregação?
Nataniel, boa tarde.
Então estou usando apenas associação simples.
já consegui resolver, mais uma vez obrigado.