JM
Dados de chave estrangeira não carregam UniqueSearch
Estou desenvolvendo um sistema de controle de abastecimento e eu preciso usar dados de outra tabela em um UniqueSearch para fazer um cadastro. Eu já fiz esse mesmo em outros formulários, porém nesse caso eu não fiz no momento de criação do formulario e inseri depois tanto no Class tanto no Forms, porém ele não funciona (mesmo estando tudo aparentemente correto).
Código do Class:
Código do Forms:
No Class do TbFrenteClass está tudo correto, tanto que funciona em outros formularios.
Código do Class:
- <?php
- <?php
- /**
- * TbAbastecimentoCidadeClass Active Record
- * @author <your-name-here>
- */
- class TbAbastecimentoCidadeClass extends TRecord
- {
- const TABLENAME = 'tb_abastecimento_cidade';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'serial'; // {max, serial}
- private $tb_frente_class;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('date');
- parent::addAttribute('litro');
- parent::addAttribute('km');
- parent::addAttribute('valor');
- parent::addAttribute('tipo');
- parent::addAttribute('posto');
- parent::addAttribute('responsavel');
- parent::addAttribute('veiculo');
- parent::addAttribute('cod_frente');
- }
- /**
- * Method set_tb_frente_class
- * Sample of usage: $tb_abastecimento_class->tb_frente_class = $object;
- * @param $object Instance of TbFrenteClass
- */
- public function set_tb_frente_class(TbFrenteClass $object)
- {
- $this->tb_frente_class = $object;
- $this->tb_frente_class_id = $object->id;
- }
- /**
- * Method get_tb_frente_class
- * Sample of usage: $tb_abastecimento_class->tb_frente_class->attribute;
- * @returns TbFrenteClass instance
- */
- public function get_tb_frente_class()
- {
- // loads the associated object
- if (empty($this->tb_frente_class))
- $this->tb_frente_class = new TbFrenteClass($this->tb_frente_class_id);
- // returns the associated object
- return $this->tb_frente_class;
- }
- }
- ?>
Código do Forms:
- <?php
- <?php
- /**
- * TbAbastecimentoCidadeClassForm Form
- * @author <your name here>
- */
- class TbAbastecimentoCidadeClassForm extends TPage
- {
- protected $form; // form
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_TbAbastecimentoCidadeClass');
- $this->form->setFormTitle('Cadastro de Abastecimento Cidade');
- // create the form fields
- $id = new TEntry('id');
- $date = new TDate('date');
- $litro = new TEntry('litro');
- $km = new TEntry('km');
- $valor = new TEntry('valor');
- $tipo = new TRadioGroup('tipo');
- $posto = new TEntry('posto');
- $responsavel = new TEntry('responsavel');
- $veiculo = new TEntry('veiculo');
- $cod_frente = new TUniqueSearch ('cod_frente', 'dbagricola', 'TbFrenteClass', 'id', 'id');
- // add the fields
- $this->form->addFields( [ new TLabel('ID') ], [ $id ] );
- $this->form->addFields( [ new TLabel('Data') ], [ $date ] );
- $this->form->addFields( [ new TLabel('Litros Abastecidos') ], [ $litro ] );
- $this->form->addFields( [ new TLabel('Km do Abastecimento') ], [ $km ] );
- $this->form->addFields( [ new TLabel('Valor') ], [ $valor ] );
- $this->form->addFields( [ new TLabel('Tipo Combustível') ], [ $tipo ] );
- $this->form->addFields( [ new TLabel('Local Abastecimento') ], [ $posto ] );
- $this->form->addFields( [ new TLabel('Responsável') ], [ $responsavel ] );
- $this->form->addFields( [ new TLabel('Veiculo') ], [ $veiculo ] );
- $this->form->addFields( [ new TLabel('Frente') ], [ $cod_frente ] );
- // add items radio
- $tipo->addItems(['Gasolina' => 'Gasolina', 'Etanol' => 'Etanol', 'Diesel Comum' => 'Diesel Comum']);
- //set length
- $cod_frente->setMinlength(0);
- // set sizes
- $id->setSize('100%');
- $date->setSize('100%');
- $litro->setSize('100%');
- $km->setSize('100%');
- $valor->setSize('100%');
- $tipo->setSize('100%');
- $posto->setSize('100%');
- $responsavel->setSize('100%');
- $veiculo->setSize('100%');
- $cod_frente->setSize('100%');
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- /** samples
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( '100%' ); // set size
- **/
- // create the form actions
- $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- // $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('dbagricola'); // 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
- $data = $this->form->getData(); // get form data as array
- $object = new TbAbastecimentoCidadeClass; // create an empty object
- $object->fromArray( (array) $data); // load the object with data
- $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', AdiantiCoreTranslator::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('dbagricola'); // open a transaction
- $object = new TbAbastecimentoCidadeClass($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
- }
- }
- }
- ?>
No Class do TbFrenteClass está tudo correto, tanto que funciona em outros formularios.
Acho que você está confundindo o TUniqueSearch com o TDBUniqueSearch. Para buscar dados automaticamente do banco de dados, use o TDBUniqueSearch. O TUniqueSearch deve ser alimentado por um array.
Corrigi aqui e realmente era isso mesmo, descuido meu, obrigado!