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.
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.
- <?php
- /**
- * AdmRequisitaForm Form
- * @author <your name here>
- */
- class AdmRequisitaForm extends TPage
- {
- protected $form; // form
-
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
-
- // creates the form
- $this->form = new TQuickForm('form_AdmRequisita');
- $this->form->class = 'tform'; // change CSS class
-
- $this->form->style = 'display: table;width:100%'; // change style
-
- // define the form title
- $this->form->setFormTitle('AdmRequisita');
-
- // create the form fields
- $id = new TEntry('id');
- $vl_solicitado = new TEntry('vl_solicitado');
- $vl_repasse = new TEntry('vl_repasse');
- $nc = new TEntry('nc');
- $nl = new TEntry('nl');
- $system_unit_id = new TEntry('system_unit_id');
- //$adm_ptres_id = new TDBCombo('adm_ptres_id','permission','AdmPtres','id','name');
- $adm_ptres_id = new TDBCombo('adm_ptres_id','permission','AdmPtres','id','{codigo}- {acao}');
- // $adm_nd_id = new TDBCombo('adm_nd_id','permission','AdmNd','id','{codigo}- {name}');
- $adm_nd_id = new TCombo('adm_nd_id');
- $system_user_id = new TEntry('system_user_id');
- // add the fields
- $this->form->addQuickField('Id', $id, 600 );
- $this->form->addQuickField('Vl Solicitado', $vl_solicitado, 600 );
- $this->form->addQuickField('Vl Repasse', $vl_repasse, 600 );
- $this->form->addQuickField('Nc', $nc, 600 );
- $this->form->addQuickField('Nl', $nl, 600 );
- $this->form->addQuickField('System Unit Id', $system_unit_id, 600 );
- $this->form->addQuickField('Adm Ptres Id', $adm_ptres_id, 600 );
- $this->form->addQuickField('Adm Nd Id', $adm_nd_id, 600 );
- $this->form->addQuickField('System User Id', $system_user_id, 600 );
- $change_action = new TAction(array($this, 'onChangeAction'));
- $adm_ptres_id->setChangeAction($change_action);
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
-
-
- $items = array();
- $adm_nd_id->addItems($items);
-
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
-
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
-
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
-
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('permission'); // open a transaction
-
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
-
- $this->form->validate(); // validate form data
-
- $object = new AdmRequisita; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->system_unit_id = TSession::getValue('userunitid');
- $object->system_user_id = TSession::getValue('userid');
- $object->store(); // save the object
-
- // get the generated id
- $data->id = $object->id;
-
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
-
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
-
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(TRUE);
- }
-
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('permission'); // open a transaction
- $object = new AdmRequisita($key); // instantiates the Active Record
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear(TRUE);
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- public static function onChangeAction($param)
- {
- $obj = new StdClass;
- TForm::sendData('form_AdmRequisita', $obj);
-
- // open database transaction
- TTransaction::open('permission');
- $criteria = new TCriteria;
- print_r($param);
- $criteria->add(new TFilter('adm_ptres_id', '=', $param['adm_ptres_id']));
- $criteria->setProperty('order', 'id');
-
- // items repository
- $repository = new TRepository('AdmPtresNd');
- // load all objects
- $collection = $repository->load($criteria);
- // add the combo items
- $items = array();
- foreach ($collection as $object)
- {
- $items[$object->adm_nd_id] = $object->adm_nd_id;
-
-
- }
- TTransaction::close();
- TCombo::reload( 'form_AdmRequisita', 'adm_nd_id', $items );
-
- }
- }
- ?>
Complementando aparecer o nome e salvar id.
Crie uma associação no model AdmPtresNd com a tabela onde está a descrição, aí pode usar conforme abaixo:
Sem palavras Nataniel, exatamente isso, muito obrigado e sucesso sempre!