Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Ajuda com TDBMultiSearch. ...
ML
Ajuda com TDBMultiSearch.  
Boa tarde. Comprei o Livro, videos e estou estudando. Porém depois de tentar por semanas, resolvi pedir ajuda.

Estou usando o TDBMultiSearch num form Master/Detalhe. Para Inclusão, está funcionando perfeito.

Mas quando eu abro a tela para Editar, no Detalhe vem todos os campos certinhos, mas o TDBMultiSearch não consigo preencher com os dados da tabela.
Print em anexo.

Desde já agradeço pela ajuda.
Abraço.

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (4)


ML



  1. <?php
  2. /**
  3.  * ConveniosTeste Master/Detail
  4.  * @author  <your name here>
  5.  */
  6. class ConveniosForm extends TPage
  7. {
  8.     protected $form// form
  9.     protected $table_details;
  10.     protected $detail_row;
  11.     
  12.     /**
  13.      * Class constructor
  14.      * Creates the page and the registration form
  15.      */
  16.     function __construct($param)
  17.     {
  18.         parent::__construct($param);
  19.         
  20.         // creates the form
  21.         $this->form = new TForm('form_ModelConvenios');
  22.         $this->form->class 'tform'// CSS class
  23.         
  24.         $table_master = new TTable;
  25.         $table_master->width '100%';
  26.         
  27.         $table_master->addRowSet( new TLabel('Convênios'), '''')->class 'tformtitle';
  28.         
  29.         // add a table inside form
  30.         $table_general = new TTable;
  31.         $table_general->width '100%';
  32.         
  33.         $frame_general = new TFrame;
  34.         $frame_general->class 'tframe tframe-custom';
  35.         $frame_general->setLegend('Convênios');
  36.         $frame_general->style 'background:whiteSmoke';
  37.         $frame_general->add($table_general);
  38.         
  39.         $frame_details = new TFrame;
  40.         $frame_details->class 'tframe tframe-custom';
  41.         $frame_details->style 'background:whiteSmoke';
  42.         $frame_details->setLegend('Serviços autorizados para o Convênio');
  43.         $frame_details->oid 'frame_details';       
  44.         
  45.         parent::include_css('app/resources/custom-frame.css');
  46.         parent::include_css('app/resources/custom-notebook.css');
  47.         parent::include_css('app/resources/custom-table.css');
  48.        
  49.         
  50.         
  51.         
  52.         $table_master->addRow()->addCell$frame_general )->colspan=2;
  53.         $row $table_master->addRow();
  54.         $row->addCell$frame_details );
  55.         
  56.         $this->form->add($table_master);
  57.         
  58.         // master fields
  59.         $id = new TEntry('id');
  60.         $descricao = new TEntry('descricao');
  61.         $observacao = new TEntry('observacao');
  62.         // sizes
  63.         $id->setSize('100');
  64.         $descricao->setSize('200');
  65.         $observacao->setSize('200');
  66.         $descricao->style  = ('text-align:left;color:#00688B;font-weight:bold;font-size:16px;');
  67.         $id->style  = ('text-align:left;color:#00688B;font-weight:bold;font-size:16px;');
  68.         $observacao->style  = ('text-align:left;color:#00688B;font-weight:bold;font-size:14px;');
  69.         if (!empty($id))
  70.         {
  71.             $id->setEditable(FALSE);
  72.         }
  73.         
  74.         // add form fields to be handled by form
  75.         $this->form->addField($id);
  76.         $this->form->addField($descricao);
  77.         $this->form->addField($observacao);
  78.         
  79.         // add form fields to the screen
  80.         $table_general->addRowSet( new TLabel('Id'), $id );
  81.         $table_general->addRowSet( new TLabel('Descricao'), $descricao );
  82.         $table_general->addRowSet( new TLabel('Observacao'), $observacao );
  83.         
  84.         // detail
  85.         $this->table_details = new TTable;
  86.         $this->table_details-> width '100%';
  87.         $frame_details->add($this->table_details);
  88.         
  89.         $this->table_details->addSection('thead');
  90.         $row $this->table_details->addRow();
  91.         
  92.         // detail header
  93.         
  94.         $row->addCell( new TLabel('Serviço'));
  95.         $row->addCell( new TLabel('Valor') );
  96.         $row->addCell( new TLabel('Co Participacao') );
  97.         $row->addCell( new TLabel('Ctafin Convenio') );
  98.         $row->addCell( new TLabel('Ctafin Cliente') );
  99.         $row->addCell( new TLabel('Empresa') );
  100.   
  101.         
  102.         
  103.         // create an action button (save)
  104.         $save_button=new TButton('save');
  105.         $save_button->setAction(new TAction(array($this'onSave')), _t('Save'));
  106.         $save_button->setImage('ico_save.png');
  107.         // create an new button (edit with no parameters)
  108.         $new_button=new TButton('new');
  109.         $new_button->setAction(new TAction(array($this'onClear')), _t('New'));
  110.         $new_button->setLabel('Novo Convênio');
  111.         $new_button->setImage('ico_new.png');
  112.         
  113.             
  114.         
  115.         
  116.         $button = new TButton('show_hide');
  117.         $button->class ='btn btn-warning btn-sm';
  118.         $button->setLabel('Mostrar/Ocultar Serviços do Convênio');
  119.         $button->addFunction("\$('[oid=frame_details]').slideToggle(); $(this).toggleClass( 'active' )");
  120.         $frame_general->add($button);
  121.         
  122.         
  123.         
  124.         
  125.         // define form fields
  126.         $this->form->addField($save_button);
  127.         $this->form->addField($new_button);
  128.         
  129.         $table_master->addRowSet( array($save_button$new_button), '''')->class 'tformaction'// CSS class
  130.         
  131.         $this->detail_row 0;
  132.         
  133.         // create the page container
  134.         $container = new TVBox;
  135.         $container->style 'width: 100%';
  136.         $container->add($this->form);
  137.         parent::add($container);
  138.     }
  139.     
  140.     
  141.     
  142.     
  143.     
  144.     
  145.     
  146.    function onEdit($param)
  147.     {
  148.          try
  149.         {
  150.             TTransaction::open('SysClinicaWeb');
  151.             
  152.             if (isset($param['key']))
  153.             {
  154.                 $key $param['key'];
  155.                 
  156.                 $object = new ModelConvenios($key);
  157.                 $this->form->setData($object);
  158.                 
  159.                 $items  ConveniosxservicosModel::where('CONVENIO_ID''='$key)->load();
  160.                 
  161.                 
  162.                  
  163.    
  164.                 
  165.                 $this->table_details->addSection('tbody');
  166.                 if ($items)
  167.                 {
  168.                     foreach($items  as $item )
  169.                     {
  170.                         $this->addDetailRow($item);                      
  171.                        
  172.                     }
  173.                     
  174.                     // create add button
  175.                     $add = new TButton('clone');
  176.                     $add->setLabel('Incluir Serviço');
  177.                     $add->class ='btn btn-warning btn-sm';
  178.                     $add->setImage('fa:plus-circle green');
  179.                     $add->addFunction('ttable_clone_previous_row(this)');
  180.                     
  181.                     // add buttons in table
  182.                     $this->table_details->addRowSet([$add]);
  183.                 }
  184.                 else
  185.                 {
  186.                     $this->onClear($param);
  187.                 }
  188.                 
  189.                 TTransaction::close(); // close transaction
  190.             }
  191.         }
  192.         catch (Exception $e// in case of exception
  193.         {
  194.             new TMessage('error'$e->getMessage());
  195.             TTransaction::rollback();
  196.         }
  197.     }
  198.      
  199.     
  200.     
  201.     
  202.     
  203.     
  204.     /**
  205.      * Add detail row
  206.      */
  207.     public function addDetailRow($item)
  208.     {
  209.    
  210.                 
  211.         $uniqid mt_rand(10000009999999);
  212.         
  213.         // create fields
  214.         $SERVICO_ID = new TDBMultiSearch('SERVICO_ID[]''SysClinicaWeb''ServicosModel''id''descricao'); 
  215.         $SERVICO_ID->setMaxSize(1);
  216.         $SERVICO_ID->setMinLength(1);
  217.         
  218.         $VALOR = new TEntry('VALOR[]');
  219.         $CO_PARTICIPACAO = new TEntry('CO_PARTICIPACAO[]');
  220.         $CTAFIN_CONVENIO = new TEntry('CTAFIN_CONVENIO[]');
  221.         $CTAFIN_CLIENTE = new TEntry('CTAFIN_CLIENTE[]');
  222.         $EMPRESA = new TEntry('EMPRESA[]');
  223.         $CONVENIO_ID = new TEntry('CONVENIO_ID[]');
  224.         
  225.         $SERVICO_ID->style  =     ('text-align:left;color:#00688B;font-weight:;font-size:15px;');
  226.         $VALOR->style  =          ('text-align:left;color:#00688B;font-weight:;font-size:15px;');
  227.         $CO_PARTICIPACAO->style = ('text-align:left;color:#00688B;font-weight:;font-size:15px;');
  228.         $CTAFIN_CLIENTE->style  = ('text-align:left;color:#00688B;font-weight:;font-size:15px;');
  229.         $CTAFIN_CONVENIO->style = ('text-align:left;color:#00688B;font-weight:;font-size:15px;');
  230.         
  231.         // set sizes
  232.         $VALOR->setSize('145');
  233.         $CO_PARTICIPACAO->setSize('100');
  234.         $CTAFIN_CONVENIO->setSize('90');
  235.         $CTAFIN_CLIENTE->setSize('90');
  236.         $EMPRESA->setSize('40');
  237.         $SERVICO_ID->setSize('300');
  238.         $CONVENIO_ID->setSize('40');
  239.                 
  240.         $VALOR->setNumericMask(2'.'',');
  241.         $CO_PARTICIPACAO->setNumericMask(2'.'',');
  242.         $EMPRESA->setEditable(FALSE);
  243.         
  244.       
  245.         // set id's
  246.         $SERVICO_ID->setId('SERVICO_ID_'.$uniqid);
  247.         $VALOR->setId('VALOR_'.$uniqid);
  248.         $CO_PARTICIPACAO->setId('CO_PARTICIPACAO_'.$uniqid);
  249.         $CTAFIN_CONVENIO->setId('CTAFIN_CONVENIO_'.$uniqid);
  250.         $CTAFIN_CLIENTE->setId('CTAFIN_CLIENTE_'.$uniqid);
  251.         $EMPRESA->setId('EMPRESA_'.$uniqid);
  252.         $CONVENIO_ID->setId('CONVENIO_ID_'.$uniqid);
  253.         
  254.         
  255.         // set row counter
  256.         $SERVICO_ID->{'data-row'} = $this->detail_row;
  257.         $CONVENIO_ID->{'data-row'} = $this->detail_row;
  258.         $VALOR->{'data-row'} = $this->detail_row;
  259.         $CO_PARTICIPACAO->{'data-row'} = $this->detail_row;
  260.         $CTAFIN_CONVENIO->{'data-row'} = $this->detail_row;
  261.         $CTAFIN_CLIENTE->{'data-row'} = $this->detail_row;
  262.         $EMPRESA->{'data-row'} = $this->detail_row;
  263.     
  264.     
  265.         
  266.         // set value
  267.         // *********  
  268.         if (!empty($item->SERVICO_ID)) { $SERVICO_ID->setValue(array($item->SERVICO_ID=>'descricao') );}
  269.         if (!empty($item->CONVENIO_ID)) { $CONVENIO_ID->setValue(array($item->CONVENIO_ID) );}
  270.         if (!empty($item->VALOR)) { $VALOR->setValue$item->VALOR ); }
  271.         if (!empty($item->CO_PARTICIPACAO)) { $CO_PARTICIPACAO->setValue$item->CO_PARTICIPACAO ); }
  272.         if (!empty($item->CTAFIN_CONVENIO)) { $CTAFIN_CONVENIO->setValue$item->CTAFIN_CONVENIO ); }
  273.         if (!empty($item->CTAFIN_CLIENTE)) { $CTAFIN_CLIENTE->setValue$item->CTAFIN_CLIENTE ); }
  274.         if (!empty($item->EMPRESA)) { $EMPRESA->setValue$item->EMPRESA ); }
  275.      
  276.         
  277.         // create delete button
  278.         $del = new TImage('fa:trash-o red');
  279.         $del->onclick 'ttable_remove_row(this)';
  280.         
  281.               
  282.         $row $this->table_details->addRow();
  283.         $row->{'data-row'} = $this->detail_row;        
  284.        
  285.  
  286.      
  287.      
  288.      
  289.         // add cells
  290.         $row->addCell($SERVICO_ID);
  291.         $row->addCell($VALOR);
  292.         $row->addCell($CO_PARTICIPACAO);
  293.         $row->addCell($CTAFIN_CONVENIO);
  294.         $row->addCell($CTAFIN_CLIENTE);
  295.         $row->addCell($EMPRESA);
  296.      //   $row->addCell($CONVENIO_ID);
  297.         $row->addCell$del ); 
  298.      
  299.         
  300.         // add form field
  301.         $this->form->addField($SERVICO_ID);
  302.         $this->form->addField($VALOR);
  303.         $this->form->addField($CO_PARTICIPACAO);
  304.         $this->form->addField($CTAFIN_CONVENIO);
  305.         $this->form->addField($CTAFIN_CLIENTE);
  306.         $this->form->addField($EMPRESA);
  307.         $this->form->addField($CONVENIO_ID);
  308.         $this->detail_row ++;
  309.     }
  310.     
  311.     
  312.     
  313.     
  314.     
  315.     
  316.     /**
  317.      * Clear form
  318.      */
  319.     public function onClear($param)
  320.     {
  321.         $this->table_details->addSection('tbody');
  322.         $this->addDetailRow( new stdClass );
  323.         
  324.         // create add button
  325.         $add = new TButton('clone');
  326.         $add->setLabel('Incluir Serviço');
  327.         $add->setImage('fa:plus-circle green');
  328.         $add->class ='btn btn-warning btn-sm';
  329.         $add->addFunction('ttable_clone_previous_row(this)');
  330.         
  331.         // add buttons in table
  332.         $this->table_details->addRowSet([$add]);
  333.     }
  334.     
  335.     /**
  336.      * Save the ModelConvenios and the ConveniosxservicosModel's
  337.      */
  338.     public static function onSave($param)
  339.     {
  340.         try
  341.         {
  342.             TTransaction::open('SysClinicaWeb');
  343.             
  344.             $id = (int) $param['id'];
  345.             $master = new ModelConvenios;
  346.             $master->fromArray$param);
  347.             $master->store(); // save master object
  348.             
  349.             // delete details
  350.             ConveniosxservicosModel::where('CONVENIO_ID''='$master->id)->delete();
  351.             
  352.             if( !empty($param['VALOR']) AND is_array($param['VALOR']) )
  353.             {
  354.                 foreach( $param['VALOR'] as $row => $VALOR)
  355.                 {
  356.                     if (!empty($VALOR))
  357.                     {
  358.                         $detail = new ConveniosxservicosModel;
  359.                         $detail->CONVENIO_ID $master->id;
  360.                         $detail->VALOR $param['VALOR'][$row];
  361.                         $detail->CO_PARTICIPACAO $param['CO_PARTICIPACAO'][$row];
  362.                         $detail->CTAFIN_CONVENIO $param['CTAFIN_CONVENIO'][$row];
  363.                         $detail->CTAFIN_CLIENTE $param['CTAFIN_CLIENTE'][$row];
  364.                         $detail->EMPRESA $param['EMPRESA'][$row];
  365.                         $detail->SERVICO_ID $param['SERVICO_ID'][$row];
  366.                         $detail->store();
  367.                     }
  368.                 }
  369.             }
  370.             
  371.             $data = new stdClass;
  372.             $data->id $master->id;
  373.             TForm::sendData('form_ModelConvenios'$data);
  374.             TTransaction::close(); // close the transaction
  375.             
  376.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  377.         }
  378.         catch (Exception $e// in case of exception
  379.         {
  380.             new TMessage('error'$e->getMessage());
  381.             TTransaction::rollback();
  382.         }
  383.     }
  384. }
  385. </your>
ML

  1. <?php
  2. /**
  3.  * ConveniosxservicosModel Active Record
  4.  * @author  <your-name-here>
  5.  */
  6. class ConveniosxservicosModel extends TRecord
  7. {
  8.     const TABLENAME 'conveniosxservicos';
  9.     const PRIMARYKEY'id';
  10.     const IDPOLICY =  'max'// {max, serial}
  11.     
  12.     
  13.     private $servicos_model;
  14.     /**
  15.      * Constructor method
  16.      */
  17.     public function __construct($id NULL$callObjectLoad TRUE)
  18.     {
  19.         parent::__construct($id$callObjectLoad);
  20.         parent::addAttribute('VALOR');
  21.         parent::addAttribute('CO_PARTICIPACAO');
  22.         parent::addAttribute('CTAFIN_CONVENIO');
  23.         parent::addAttribute('CTAFIN_CLIENTE');
  24.         parent::addAttribute('EMPRESA');
  25.         parent::addAttribute('SERVICO_ID');
  26.         parent::addAttribute('CONVENIO_ID');
  27.   
  28.         
  29.     }
  30.   
  31. }
NR

Você precisa passar um array associativo(chave,descricao) para o multisearch. Crie uma associação com o modelo correspondente ao SERVICO_ID no modelo ConveniosxservicosModel e aí modifique o trecho abaixo:
  1. <?php
  2. //if (!empty($item->SERVICO_ID)) { $SERVICO_ID->setValue(array($item->SERVICO_ID=>'descricao') );}
  3. // supondo que exista uma função get_servico(associacao) no ConveniosxservicosModel e no modelo da associacao exista uma coluna chamada descricao
  4. if (!empty($item->SERVICO_ID)) { $SERVICO_ID->setValue(array($item->SERVICO_ID=>$item->servico->descricao) );}
  5. ?>
ML

Muito Obrigado Nataniel.

Ótima explicação. Agora ta funcionando perfeitamente.