Obter atributo nome em TCombo de uma tabela de agregação. Saudações, Como obter retorno do atributo nome em uma combo estabelecida por critérios da TDBCombo anterior? Está filtrando certinho só que a segunda combo apresenta o id e eu preciso do nome, ajuda nós... Saliento que os dados da combo que apresenta somente o id vem de uma tabela de agregação. ...
JC
Obter atributo nome em TCombo de uma tabela de agregação.  
Saudações,

Como obter retorno do atributo nome em uma combo estabelecida por critérios da TDBCombo anterior? Está filtrando certinho só que a segunda combo apresenta o id e eu preciso do nome, ajuda nós... Saliento que os dados da combo que apresenta somente o id vem de uma tabela de agregação.

 
  1. <?php
  2. /**
  3. * AdmRequisitaForm Form
  4. * @author <your name here>
  5. */
  6. class AdmRequisitaForm extends TPage
  7. {
  8. protected $form; // form
  9. /**
  10. * Form constructor
  11. * @param $param Request
  12. */
  13. public function __construct( $param )
  14. {
  15. parent::__construct();
  16. // creates the form
  17. $this->form = new TQuickForm('form_AdmRequisita');
  18. $this->form->class = 'tform'; // change CSS class
  19. $this->form->style = 'display: table;width:100%'; // change style
  20. // define the form title
  21. $this->form->setFormTitle('AdmRequisita');
  22. // create the form fields
  23. $id = new TEntry('id');
  24. $vl_solicitado = new TEntry('vl_solicitado');
  25. $vl_repasse = new TEntry('vl_repasse');
  26. $nc = new TEntry('nc');
  27. $nl = new TEntry('nl');
  28. $system_unit_id = new TEntry('system_unit_id');
  29. //$adm_ptres_id = new TDBCombo('adm_ptres_id','permission','AdmPtres','id','name');
  30. $adm_ptres_id = new TDBCombo('adm_ptres_id','permission','AdmPtres','id','{codigo}- {acao}');
  31. // $adm_nd_id = new TDBCombo('adm_nd_id','permission','AdmNd','id','{codigo}- {name}');
  32. $adm_nd_id = new TCombo('adm_nd_id');
  33. $system_user_id = new TEntry('system_user_id');
  34. // add the fields
  35. $this->form->addQuickField('Id', $id, 600 );
  36. $this->form->addQuickField('Vl Solicitado', $vl_solicitado, 600 );
  37. $this->form->addQuickField('Vl Repasse', $vl_repasse, 600 );
  38. $this->form->addQuickField('Nc', $nc, 600 );
  39. $this->form->addQuickField('Nl', $nl, 600 );
  40. $this->form->addQuickField('System Unit Id', $system_unit_id, 600 );
  41. $this->form->addQuickField('Adm Ptres Id', $adm_ptres_id, 600 );
  42. $this->form->addQuickField('Adm Nd Id', $adm_nd_id, 600 );
  43. $this->form->addQuickField('System User Id', $system_user_id, 600 );
  44. $change_action = new TAction(array($this, 'onChangeAction'));
  45. $adm_ptres_id->setChangeAction($change_action);
  46. if (!empty($id))
  47. {
  48. $id->setEditable(FALSE);
  49. }
  50. $items = array();
  51. $adm_nd_id->addItems($items);
  52. /** samples
  53. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  54. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  55. $fieldX->setSize( 100, 40 ); // set size
  56. **/
  57. // create the form actions
  58. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  59. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  60. // vertical box container
  61. $container = new TVBox;
  62. $container->style = 'width: 90%';
  63. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  64. $container->add($this->form);
  65. parent::add($container);
  66. }
  67. /**
  68. * Save form data
  69. * @param $param Request
  70. */
  71. public function onSave( $param )
  72. {
  73. try
  74. {
  75. TTransaction::open('permission'); // open a transaction
  76. /**
  77. // Enable Debug logger for SQL operations inside the transaction
  78. TTransaction::setLogger(new TLoggerSTD); // standard output
  79. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  80. **/
  81. $this->form->validate(); // validate form data
  82. $object = new AdmRequisita; // create an empty object
  83. $data = $this->form->getData(); // get form data as array
  84. $object->fromArray( (array) $data); // load the object with data
  85. $object->system_unit_id = TSession::getValue('userunitid');
  86. $object->system_user_id = TSession::getValue('userid');
  87. $object->store(); // save the object
  88. // get the generated id
  89. $data->id = $object->id;
  90. $this->form->setData($data); // fill form data
  91. TTransaction::close(); // close the transaction
  92. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  93. }
  94. catch (Exception $e) // in case of exception
  95. {
  96. new TMessage('error', $e->getMessage()); // shows the exception error message
  97. $this->form->setData( $this->form->getData() ); // keep form data
  98. TTransaction::rollback(); // undo all pending operations
  99. }
  100. }
  101. /**
  102. * Clear form data
  103. * @param $param Request
  104. */
  105. public function onClear( $param )
  106. {
  107. $this->form->clear(TRUE);
  108. }
  109. /**
  110. * Load object to form data
  111. * @param $param Request
  112. */
  113. public function onEdit( $param )
  114. {
  115. try
  116. {
  117. if (isset($param['key']))
  118. {
  119. $key = $param['key']; // get the parameter $key
  120. TTransaction::open('permission'); // open a transaction
  121. $object = new AdmRequisita($key); // instantiates the Active Record
  122. $this->form->setData($object); // fill the form
  123. TTransaction::close(); // close the transaction
  124. }
  125. else
  126. {
  127. $this->form->clear(TRUE);
  128. }
  129. }
  130. catch (Exception $e) // in case of exception
  131. {
  132. new TMessage('error', $e->getMessage()); // shows the exception error message
  133. TTransaction::rollback(); // undo all pending operations
  134. }
  135. }
  136. public static function onChangeAction($param)
  137. {
  138. $obj = new StdClass;
  139. TForm::sendData('form_AdmRequisita', $obj);
  140. // open database transaction
  141. TTransaction::open('permission');
  142. $criteria = new TCriteria;
  143. print_r($param);
  144. $criteria->add(new TFilter('adm_ptres_id', '=', $param['adm_ptres_id']));
  145. $criteria->setProperty('order', 'id');
  146. // items repository
  147. $repository = new TRepository('AdmPtresNd');
  148. // load all objects
  149. $collection = $repository->load($criteria);
  150. // add the combo items
  151. $items = array();
  152. foreach ($collection as $object)
  153. {
  154. $items[$object->adm_nd_id] = $object->adm_nd_id;
  155. }
  156. TTransaction::close();
  157. TCombo::reload( 'form_AdmRequisita', 'adm_nd_id', $items );
  158. }
  159. }
  160. ?>

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)


JC

Complementando aparecer o nome e salvar id.
NR

Crie uma associação no model AdmPtresNd com a tabela onde está a descrição, aí pode usar conforme abaixo:
 
  1. <?php
  2. $items[$object->adm_nd_id] = $object->adm_nd->descricao;
  3. ?>
JC

Sem palavras Nataniel, exatamente isso, muito obrigado e sucesso sempre!