MC
TDBMultiSearch em Form Mestre/Detalhe
Galera, mais uma dúvida.
Estou montando um formulário mestre detalhe.
Tenho como mestre a tabela da ESCALA (id, descricao, dataEscala) e como detalhe a tabela ESCALA_MEMBROS (id, escala_id, membro_id, funcao_id).
Preciso vincular vários membros a uma determinada escala com uma função.
Utilizei como base o exemplo do tutor (https://www.adianti.com.br/framework_files/tutor/index.php?class=SaleMultiValueF).
Porém, no meu formulário está sendo possível incluir num único TDBMultiSearch mais de um registro. Gostaria de alterar para o comportamento ficar igual ao modelo do Tutor. Já tentei identificar como fazer pelo código fonte mas não consegui sucesso.
Segue minha classe do formulário mestre detalhe:
Estou montando um formulário mestre detalhe.
Tenho como mestre a tabela da ESCALA (id, descricao, dataEscala) e como detalhe a tabela ESCALA_MEMBROS (id, escala_id, membro_id, funcao_id).
Preciso vincular vários membros a uma determinada escala com uma função.
Utilizei como base o exemplo do tutor (https://www.adianti.com.br/framework_files/tutor/index.php?class=SaleMultiValueF).
Porém, no meu formulário está sendo possível incluir num único TDBMultiSearch mais de um registro. Gostaria de alterar para o comportamento ficar igual ao modelo do Tutor. Já tentei identificar como fazer pelo código fonte mas não consegui sucesso.
Segue minha classe do formulário mestre detalhe:
- <?php
- /**
- * EscalaDataForm Master/Detail
- * @author <your name here>
- */
- class EscalaDataForm extends TPage
- {
- protected $form; // form
- protected $table_details;
- protected $detail_row;
-
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct($param)
- {
- parent::__construct($param);
-
- // creates the form
- $this->form = new TForm('form_EscalaData');
- $this->form->class = 'tform'; // CSS class
-
- $table_master = new TTable;
- $table_master->width = '100%';
-
- $table_master->addRowSet( new TLabel('EscalaData'), '', '')->class = 'tformtitle';
-
- // add a table inside form
- $table_general = new TTable;
- $table_general->width = '100%';
-
- $frame_general = new TFrame;
- $frame_general->class = 'tframe tframe-custom';
- $frame_general->setLegend('EscalaData');
- $frame_general->style = 'background:whiteSmoke';
- $frame_general->add($table_general);
-
- $frame_details = new TFrame;
- $frame_details->class = 'tframe tframe-custom';
- $frame_details->setLegend('Escala_Membro');
-
- $table_master->addRow()->addCell( $frame_general )->colspan=2;
- $row = $table_master->addRow();
- $row->addCell( $frame_details );
-
- $this->form->add($table_master);
-
- // master fields
- $id = new TEntry('id');
- $escala_id = new ">TDBSeekButton('escala_id', 'mvadmin', 'form_EscalaData', 'escala', 'descricao', 'escala_id', 'escalaNome');
- $escalaNome = new TEntry('escalaNome');
- $dataEsc = new TDate('dataEsc');
- $escala_id->setSize(100);
- $escalaNome->setSize(300);
- $escalaNome->setEditable(FALSE);
- // sizes
- $id->setSize('100');
- $escala_id->setSize('100');
- $escalaNome->setSize('200');
- $dataEsc->setSize('100');
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
-
- // add form fields to be handled by form
- $this->form->addField($id);
- $this->form->addField($escala_id);
- $this->form->addField($escalaNome);
- $this->form->addField($dataEsc);
-
- // add form fields to the screen
- $table_general->addRowSet( new TLabel('Sequência'), $id );
- $table_general->addRowSet( $label_Escala = new TLabel('Escala'), array( $escala_id, $escalaNome ) );
- $table_general->addRowSet( new TLabel('Data'), $dataEsc );
-
- // detail
- $this->table_details = new TTable;
- $this->table_details-> width = '100%';
- $frame_details->add($this->table_details);
-
- $this->table_details->addSection('thead');
- $row = $this->table_details->addRow();
-
- // detail header
- $row->addCell( new TLabel('Membro') );
- $row->addCell( new TLabel('Função') );
-
- // create an action button (save)
- $save_button=new TButton('save');
- $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
- $save_button->setImage('ico_save.png');
- // create an new button (edit with no parameters)
- $new_button=new TButton('new');
- $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
- $new_button->setImage('ico_new.png');
-
- // define form fields
- $this->form->addField($save_button);
- $this->form->addField($new_button);
-
- $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
-
- $this->detail_row = 0;
-
- // create the page container
- $container = new TVBox;
- $container->style = 'width: 100%';
- //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
-
- /**
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- TTransaction::open('mvadmin');
-
- if (isset($param['key']))
- {
- $key = $param['key'];
-
- $object = new EscalaData($key);
- $this->form->setData($object);
-
- $items = Escala_Membro::where('escala_id', '=', $key)->load();
-
- $this->table_details->addSection('tbody');
- if ($items)
- {
- foreach($items as $item )
- {
- $this->addDetailRow($item);
- }
-
- // create add button
- $add = new TButton('clone');
- $add->setLabel('Add');
- $add->setImage('fa:plus-circle green');
- $add->addFunction('ttable_clone_previous_row(this)');
-
- // add buttons in table
- $this->table_details->addRowSet([$add]);
- }
- else
- {
- $this->onClear($param);
- }
-
- TTransaction::close(); // close transaction
- }
- }
-
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- try
- {
- if (isset($param['id']))
- {
- // open a transaction with database 'mvadmin'
- TTransaction::open('mvadmin');
- // get the parameter $key
- $key = $param['id'];
- // instantiates object Escala
- $object = new EscalaData($key);
-
- $object->escalaNome = $object->escala->descricao;
-
- // fill the form with the active record data
- $this->form->setData($object);
- // close the transaction
- TTransaction::close();
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', 'Error ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
-
- /**
- * Add detail row
- */
- public function addDetailRow($item)
- {
- $uniqid = mt_rand(1000000, 9999999);
-
- // create fields
- $membro_id = new TDBMultiSearch('membro_id[]', 'mvadmin', 'membro', 'id', 'nome');
- $funcao_id = new TDBMultiSearch('funcao_id[]', 'mvadmin', 'funcao', 'id', 'descricao');
- // set id's
- $membro_id->setId('membro_id_'.$uniqid);
- $funcao_id->setId('funcao_id_'.$uniqid);
- // set sizes
- $membro_id->setSize('200');
- $funcao_id->setSize('200');
-
- // set row counter
- $membro_id->{'data-row'} = $this->detail_row;
- $funcao_id->{'data-row'} = $this->detail_row;
- // set value
- if (!empty($item->membro_id)) { $membro_id->setValue( $item->membro_id ); }
- if (!empty($item->funcao_id)) { $funcao_id->setValue( $item->funcao_id ); }
-
- // create delete button
- $del = new TImage('fa:trash-o red');
- $del->onclick = 'ttable_remove_row(this)';
-
- $row = $this->table_details->addRow();
- // add cells
- $row->addCell($membro_id);
- $row->addCell($funcao_id);
-
- $row->addCell( $del );
- $row->{'data-row'} = $this->detail_row;
-
- // add form field
- $this->form->addField($membro_id);
- $this->form->addField($funcao_id);
-
- $this->detail_row ++;
- }
-
- /**
- * Clear form
- */
- public function onClear($param)
- {
- $this->table_details->addSection('tbody');
- $this->addDetailRow( new stdClass );
-
- // create add button
- $add = new TButton('clone');
- $add->setLabel('Add');
- $add->setImage('fa:plus-circle green');
- $add->addFunction('ttable_clone_previous_row(this)');
-
- // add buttons in table
- $this->table_details->addRowSet([$add]);
- }
-
- /**
- * Save the EscalaData and the Escala_Membro's
- */
- public static function onSave($param)
- {
- try
- {
- TTransaction::open('mvadmin');
-
- $id = (int) $param['id'];
- $master = new EscalaData;
- $master->fromArray( $param);
- $master->store(); // save master object
-
- // delete details
- Escala_Membro::where('escala_id', '=', $master->id)->delete();
-
- if( !empty($param['membro_id']) AND is_array($param['membro_id']) )
- {
- foreach( $param['membro_id'] as $row => $membro_id)
- {
- if (!empty($membro_id))
- {
- $detail = new Escala_Membro;
- $detail->escala_id = $master->id;
- $detail->membro_id = $param['membro_id'][$row];
- $detail->funcao_id = $param['funcao_id'][$row];
- $detail->store();
- }
- }
- }
-
- $data = new stdClass;
- $data->id = $master->id;
- TForm::sendData('form_EscalaData', $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());
- TTransaction::rollback();
- }
- }
- }
- ?>
Use a função setMaxSize(1):
Bom dia!
Deu certo Nataniel. Muito obrigado!
Porém, estou tendo outros problemas.
Ao editar um registro, os itens não estão sendo carregados para o TMultiSearch. Fica tudo como "null" e acima da página exibe uma mensagem de erro.
Segue link com print.
imgur.com/a/y5eUS
Você precisa passar um array associativo para a função setValue:
Perfeito. Realmente, revendo o código de exemplo estava claro.
Inclui o array associativo e também precisei criar os métodos get e set membro na classe model relacionada.
Obrigado Nataniel!