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




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

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework 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.