Relacionamento entre tabelas Boa tarde, Por favor, como fazer o relacionamento entre duas tabelas. Por exemplo: buscar a cidade do cliente em outra tabela cidade, Segue, anexo, imagem do fornulário. Veja o código que estou utilizando: ...
ST
Relacionamento entre tabelas  
Boa tarde,
Por favor, como fazer o relacionamento entre duas tabelas.
Por exemplo: buscar a cidade do cliente em outra tabela cidade,

Segue, anexo, imagem do fornulário.

Veja o código que estou utilizando:

 
  1. <?php
  2. /**
  3. * BookList Listing
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage library
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class ClienteList extends TStandardList
  13. {
  14. protected $form; // registration form
  15. protected $datagrid; // listing
  16. protected $pageNavigation;
  17. protected $loaded;
  18. /**
  19. * Class constructor
  20. * Creates the page, the form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. // defines the database
  26. parent::setDatabase('teste');
  27. // defines the active record
  28. parent::setActiveRecord('cliente');
  29. // defines the filter field
  30. parent::addFilterField('cliente_id', 'like', 'cliente_id');
  31. parent::addFilterField('nomecliente', '=', 'nomecliente');
  32. parent::addFilterField('cidade_id', '=', 'cidade_id');
  33. // creates the form
  34. $this->form = new BootstrapFormBuilder('form_search_Cliente');
  35. $this->form->setFormTitle('');
  36. // create the form fields
  37. $cliente_id = new TEntry('cliente_id');
  38. $title = new TEntry('nomecliente');
  39. $cidade_id = new ">TDBSeekButton('cidade_id', 'teste', $this->form->getName(), 'Cidade', 'nomecidade', 'cidade_id', 'nomecidade');
  40. $nomecidade = new TEntry('cidades->nomecidade');
  41. $nomecidade->setEditable(FALSE);
  42. $cliente_id->setSize('10%');
  43. $title->setSize('100%');
  44. $cidade_id->setSize('10%');
  45. $nomecidade->setSize('80%');
  46. $this->form->addFields( [ new TLabel(_t('Code')) ], [ $cliente_id ] );
  47. $this->form->addFields( [ new TLabel(_t('Name')) ], [ $title ] );
  48. $this->form->addFields( [ new TLabel(_t('City')) ], [ $cidade_id, $nomecidade ] );
  49. $this->form->addAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
  50. $this->form->addAction(_t('New'), new TAction(array('ClienteForm', 'onClear')), 'fa:plus-square green');
  51. $this->form->setData(TSession::getValue('Book_filter_data'));
  52. // creates a DataGrid
  53. $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  54. $this->datagrid->style = 'width: 100%';
  55. $this->datagrid->datatable = 'true';
  56. // creates the datagrid columns
  57. $id = new TDataGridColumn('cliente_id','Código', 'right', 100);
  58. $title = new TDataGridColumn('nomecliente','Nome', 'left',200 );
  59. $nomecidade = new TDataGridColumn('nomecidade','Cidade','left', 190);
  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', 'cliente_id');
  65. $order2->setParameter('order', 'nomecliente');
  66. // assign the ordering actions
  67. $id->setAction($order1);
  68. $title->setAction($order2);
  69. // add the columns to the DataGrid
  70. $this->datagrid->addColumn($id);
  71. $this->datagrid->addColumn($title);
  72. $this->datagrid->addColumn($nomecidade);
  73. // creates two datagrid actions
  74. $action1 = new TDataGridAction(array('ClienteForm', 'onEdit'));
  75. $action1->setLabel(_t('Edit'));
  76. $action1->setImage('fa:pencil-square-o blue fa-lg');
  77. $action1->setField('cliente_id');
  78. $action2 = new TDataGridAction(array($this, 'onDelete'));
  79. $action2->setLabel(_t('Delete'));
  80. $action2->setImage('fa:trash-o red fa-lg');
  81. $action2->setField('cliente_id');
  82. // add the actions to the datagrid
  83. $this->datagrid->addAction($action1);
  84. $this->datagrid->addAction($action2);
  85. // create the datagrid model
  86. $this->datagrid->createModel();
  87. // creates the page navigation
  88. $this->pageNavigation = new TPageNavigation;
  89. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  90. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  91. // creates the page structure using a vbox
  92. $container = new TVBox;
  93. $container->style = 'width: 100%';
  94. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  95. $container->add($this->form);
  96. $container->add(TPanelGroup::pack('', $this->datagrid));
  97. $container->add($this->pageNavigation);
  98. // add the vbox inside the page
  99. parent::add($container);
  100. }
  101. }

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 (3)


MG

O relacionamento é feito no "model".
Vc usa o Adianti Pró?
Através dele, vc faz o relacionando no Model Cliente, apontando para o Model Cidade.
ST

Bom dia,
Utilizo o Adianti Pro mas não estou conseguindo listar os dados no dbgrid.
MG

Posta os models: Cliente e Cidade para podermos ajudar!