Pegar dados de tabela relacionada Olá, Estou tentado carregar item de tabela relacionada ( contatoweb), porém sem sucesso. Segue foonte abaixo: Grid onde estou tentando carregar os dados ...
RB
Pegar dados de tabela relacionada  
Olá,

Estou tentado carregar item de tabela relacionada ( contatoweb), porém sem sucesso.


Segue foonte abaixo:
Grid onde estou tentando carregar os dados

 
  1. <?php
  2. class DatagridCheckView extends TPage
  3. {
  4. private $datagrid;
  5. private $pageNavigation;
  6. private $loaded;
  7. private $form;
  8. private $editar;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->datagrid = new TDataGrid;
  13. //$this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  14. $this->datagrid->style="width: 100%"; // Responsive
  15. $telefone = new TDataGridColumn('pessoa->contato->telefone','Telefone','left'); //esta linha esta funcionando
  16. $email = new TDataGridColumn('pessoa->contato_web->email','E-mail','left');// esta linha não funciona
  17. $this->datagrid->addColumn($telefone);
  18. $this->datagrid->addColumn($email);
  19. $this->datagrid->addColumn($site);
  20. $this->datagrid->createModel();
  21. ....
  22. ?>

model cliente

 
  1. <?php
  2. class Cliente extends TRecord
  3. {
  4. const TABLENAME = 'cliente';
  5. const PRIMARYKEY= 'id';
  6. const IDPOLICY = 'serial'; // {max, serial}
  7. private $pessoa;
  8. /**
  9. * Constructor method
  10. */
  11. public function __construct($id = NULL, $callObjectLoad = TRUE)
  12. {
  13. parent::__construct($id, $callObjectLoad);
  14. parent::addAttribute('pessoa_id');
  15. parent::addAttribute('organizacao_id');
  16. parent::addAttribute('criacao');
  17. parent::addAttribute('atualizacao');
  18. parent::addAttribute('ativo');
  19. }
  20. /**
  21. * Method set_pessoa
  22. * Sample of usage: $cliente->pessoa = $object;
  23. * @param $object Instance of Pessoa
  24. */
  25. public function set_pessoa(Pessoa $object)
  26. {
  27. $this->pessoa = $object;
  28. $this->pessoa_id = $object->id;
  29. }
  30. /**
  31. * Method get_pessoa
  32. * Sample of usage: $cliente->pessoa->attribute;
  33. * @returns Pessoa instance
  34. */
  35. public function get_pessoa()
  36. {
  37. // loads the associated object
  38. if (empty($this->pessoa))
  39. $this->pessoa = new Pessoa($this->pessoa_id);
  40. // returns the associated object
  41. return $this->pessoa;
  42. }
  43. ?>


model Pessoa

 
  1. <?php
  2. class Pessoa extends TRecord
  3. {
  4. const TABLENAME = 'pessoa';
  5. const PRIMARYKEY= 'id';
  6. const IDPOLICY = 'serial'; // {max, serial}
  7. private $pessoa;
  8. private $contato;
  9. private $contato_web;
  10. /**
  11. * Constructor method
  12. */
  13. public function __construct($id = NULL, $callObjectLoad = TRUE)
  14. {
  15. parent::__construct($id, $callObjectLoad);
  16. parent::addAttribute('nome');
  17. parent::addAttribute('data_nascimento');
  18. parent::addAttribute('organizacao_id');
  19. parent::addAttribute('criacao');
  20. parent::addAttribute('atualizacao');
  21. parent::addAttribute('ativo');
  22. parent::addAttribute('tipo_pessoa_id');
  23. }
  24. public function get_contato()
  25. {
  26. if (empty($this->contato))
  27. {
  28. $contato = Contato::where('pessoa_id','=',$this->id)
  29. ->where($this->tipo_pessoa_id,'=',1)->load();
  30. $this->contato = $contato[0];
  31. }
  32. return $this->contato;
  33. }
  34. /**
  35. * Method get_contato_web
  36. * Sample of usage: $pessoa->contato_web->attribute;
  37. * @returns Contatoweb instance
  38. */
  39. public function get_contato_web()
  40. {
  41. if (empty($this->contato_web))
  42. {
  43. $contatoweb = ContatoWeb::where('pessoa_id','=',$this->id)
  44. ->where($this->tipo_pessoa_id,'=',1)
  45. ->load();
  46. $this->contatoweb = $contatoweb[0];
  47. }
  48. return $this->contato_web;
  49. }
  50. }
  51. ?>


Model ContatoWeb
 
  1. <?php
 
  1. <?php
  2. /**
  3. * Contatoweb Active Record
  4. * @author <your-name-here>
  5. */
  6. class ContatoWeb extends TRecord
  7. {
  8. const TABLENAME = 'contatoweb';
  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('email');
  20. parent::addAttribute('site');
  21. }
  22. /**
  23. * Method set_pessoa
  24. * Sample of usage: $contatoweb->pessoa = $object;
  25. * @param $object Instance of Pessoa
  26. */
  27. public function set_pessoa(Pessoa $object)
  28. {
  29. $this->pessoa = $object;
  30. $this->pessoa_id = $object->id;
  31. }
  32. /**
  33. * Method get_pessoa
  34. * Sample of usage: $contatoweb->pessoa->attribute;
  35. * @returns Pessoa instance
  36. */
  37. public function get_pessoa()
  38. {
  39. // loads the associated object
  40. if (empty($this->pessoa))
  41. $this->pessoa = new Pessoa($this->pessoa_id);
  42. // returns the associated object
  43. return $this->pessoa;
  44. }
  45. }
  46. ?>


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

Acho que é erro de digitação:
 
  1. <?php
  2. // deveria ser $this->contato_web = $contatoweb[0]
  3. $this->contatoweb = $contatoweb[0];
  4. ?>
RB

Nataniel,

To ficando maluco....

já ajustei porem sem sucesso...

segue trecho Pessoa
 
  1. <?php
  2. .....
  3. private $contatoweb;
  4. public function get_contatoweb()
  5. {
  6. if (empty($this->contatoweb))
  7. {
  8. $contato_web = ContatoWeb::where('pessoa_id','=',$this->id)->load();
  9. $this->contatoweb = $contato_web[0];
  10. }
  11. return $this->contatoweb;
  12. }
  13. ?>



Grid

 
  1. <?php
  2. $email = new TDataGridColumn('pessoa->contatoweb->email','E-mail','left');
  3. $site = new TDataGridColumn('pessoa->contatoweb->site','Site','left');
  4. ?>