Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Gravação de TFieldList ...
MA
Gravação de TFieldList  


  1. <?php
  2. /**
  3.  * Evento_aplicacaoForm Registration
  4.  * @author  <your name here>
  5.  */
  6. class Evento_aplicacaoForm extends TPage
  7. {
  8.     protected $form// form
  9.     protected $fieldlist;
  10.     
  11.     /**
  12.      * Class constructor
  13.      * Creates the page and the registration form
  14.      */
  15.     function __construct()
  16.     {
  17.         parent::__construct();
  18.         
  19.         // creates the form
  20.         $this->form = new TForm('form_Evento_aplicacao');
  21.         $this->form->class 'tform'// CSS class
  22.         $this->form->style 'width: 100%';
  23.         
  24.         // add a table inside form
  25.         $table = new TTable;
  26.         $table-> width '100%';
  27.         $this->form->add($table);
  28.         
  29.         // add a row for the form title
  30.         $row $table->addRow();
  31.         $row->class 'tformtitle'// CSS class
  32.         $row->addCell( new TLabel('Formulário - Evento Específico') )->colspan 6;
  33.         
  34.         // create the form fields
  35.         $id                             = new TEntry('id');
  36.         $empreendimento_id              = new TDBCombo('empreendimento_id','sqlserver','Empreendimento','id','sigla');
  37.         $aplicacao_id                   = new TCombo('aplicacao_id');
  38.         $evento                         = new TEntry('evento');
  39.         $grupo_id                       = new TDBCombo('grupo_id''sqlserver','Grupo','id','descricao');
  40.         $indicador                      = new TCombo('indicador');
  41.         
  42.         // set change action for empreendimento
  43.         $change_action = new TAction(array($this'onChange'));
  44.         $empreendimento_id->setChangeAction($change_action);
  45.         
  46.         // define the sizes
  47.         $id->setSize(100);
  48.         $id->setEditable(false);
  49.         $evento->setSize(500);
  50.         $grupo_id->setSize(200);
  51.         $aplicacao_id->setSize(200);
  52.         $empreendimento_id->setSize(200);
  53.         $indicador->setSize(100);
  54.         
  55.         $items = array();
  56.         $items['1'] ='Sim';
  57.         $items['0'] ='Não';
  58.         
  59.         $indicador->addItems($items);
  60.         // validations
  61.         $evento->addValidation('Evento', new TRequiredValidator);
  62.         $grupo_id->addValidation('Grupo', new TRequiredValidator);
  63.         $aplicacao_id->addValidation('Aplicação', new TRequiredValidator);
  64.         $empreendimento_id->addValidation('Empreendimento', new TRequiredValidator);
  65.         $indicador->addValidation('Indicador?', new TRequiredValidator);
  66.         
  67.         $row $table->addRow();
  68.         $row->addCell(new TLabel('ID:'));
  69.         $row->addCell($id);
  70.         $row->addCell($label_empreendimento_id = new TLabel('Empreendimento:'));
  71.         $row->addCell($empreendimento_id);
  72.         $row->addCell($label_aplicacao_id = new TLabel('Aplicação:'));
  73.         $row->addCell($aplicacao_id);
  74.         $row $table->addRow();
  75.         $row->addCell($label_evento = new TLabel('Evento:'));
  76.         $row->addCell($evento)->colspan 3;
  77.         $row->addCell($label_grupo_id = new TLabel('Grupo:'));
  78.         $row->addCell($grupo_id);
  79.         $row $table->addRow();
  80.         $row->addCell($label_indicador = new TLabel('Indicador?'));
  81.         $row->addCell($indicador);
  82.         
  83.         $label_evento->setFontColor('#FF0000');
  84.         $label_grupo_id->setFontColor('#FF0000');
  85.         $label_aplicacao_id->setFontColor('#FF0000');
  86.         $label_empreendimento_id->setFontColor('#FF0000');
  87.         $label_indicador->setFontColor('#FF0000');
  88.         
  89.         //--------dados do frame---------
  90.         //##################################
  91.         $frame_solucao = new TFrame(NULL350);
  92.         $frame_solucao->setLegend('Soluções para o evento');
  93.         $frame_solucao->style .= ';margin: 4px';
  94.         
  95.       //  $solucao = new TMultiField('solucao');
  96.       //  $solucao->setClass('Solucao');
  97.       
  98.         $this->fieldlist = new TFieldList;
  99.         $this->fieldlist->name =  'Solucao';        
  100.         $this->fieldlist->width '100%';
  101. //      $this->form->add($this->fieldlist);
  102.         $this->fieldlist->generateAria();
  103.                 
  104.         $solucao_id = new TEntry('solucao_id');
  105.         $solucao_id->setSize(50);
  106.         //$solucao_id->setEditable(false);
  107.         $solucao_descricao        = new TEntry('descricao');
  108.         $solucao_descricao->setSize(600);
  109.         $this->fieldlist->addHeader();
  110.         $this->fieldlist->enableSorting();
  111.         
  112. //     $solucao->setHeight(200);
  113.         
  114.         $this->fieldlist->addField('<b>ID</br>',$solucao_id , ['width' => '50%'] );         
  115.         $this->fieldlist->addField('<b>Descrição</br>'$solucao_descricao, ['width' => '200%']);         
  116.         
  117. //      $solucao->addField('id', 'ID',  $solucao_id, 50);
  118. //      $solucao->addField('descricao',('Solução'), $solucao_descricao, 600);
  119. //      $solucao->setOrientation('horizontal');
  120.     
  121. //        $this->form->addContent( [new TLabel('Solucao')], [$this->fieldlist]);
  122.     
  123.         $frame_solucao->add($this->fieldlist);
  124.         
  125.         $row=$table->addRow();
  126.         $cell $row->addCell($frame_solucao);
  127.         $cell->colspan 6;
  128.         
  129.         
  130.         //----------------------------------------------------------------------------------
  131.         
  132.         $this->form->setFields(array($id,$evento,$grupo_id,$aplicacao_id,$empreendimento_id,$indicador));
  133.         //$this->form->addContent( [$this->fieldlist] );
  134.         // create the form actions
  135.         $save_button TButton::create('save', array($this'onSave'), _t('Save'), 'ico_save.png');
  136.         $new_button  TButton::create('new',  array($this'onEdit'), _t('New'),  'far:edit blue fa-lg');
  137.         $list_button  TButton::create('list', array('Evento_aplicacaoList''onReload'), (_t('Back to the listing')),  'ico_datagrid.png');
  138.         
  139.         $this->form->addField($save_button);
  140.         $this->form->addField($new_button);
  141.         $this->form->addField($list_button);
  142.         
  143.         $buttons_box = new THBox;
  144.         $buttons_box->add($save_button);
  145.         $buttons_box->add($new_button);
  146.         $buttons_box->add($list_button);
  147.         
  148.         // add a row for the form action
  149.         $row $table->addRow();
  150.         $row->class 'tformaction'// CSS class
  151.         $row->addCell($buttons_box)->colspan 6;
  152.         
  153.         $container = new TTable;
  154.         $container->style 'width: 80%';
  155.         $container->addRow()->addCell(new TXMLBreadCrumb('menu.xml''Evento_aplicacaoList'));
  156.         $container->addRow()->addCell($this->form);
  157.         // add the form to the page_
  158.         parent::add($container);
  159.     }
  160.     /**
  161.      * method onSave()
  162.      * Executed whenever the user clicks at the save button
  163.      */
  164.     function onSave ($param)
  165.     {
  166.         try 
  167.         {
  168.            var_dump($param);
  169.         
  170.             TTransaction::open('sqlserver'); // open a transaction
  171.             
  172.             // get the form data into an active record Evento_aplicacao
  173.             $object $this->form->getData('Evento_aplicacao');
  174.         
  175.             $this->form->validate(); // form validation
  176.             
  177.             if( $object->solucao ) {
  178.             
  179.                 foreach( $object->solucao as $solucao ){
  180.                 
  181.                     $object->addSolucao$solucao );
  182.                     //print_r($solucao);
  183.                 }
  184.             }
  185.       if( !empty($param['evento_aplicacao_id']) && is_array($param['evento_aplicacao_id']) )
  186.       
  187.       {  
  188.             foreach( $param['evento_aplicacao_id'] as $row => $descricao
  189.             {
  190.             
  191.             $solucao = new Solucao;
  192.             $solucao->id $param['solucao_id'][$row];
  193.             $solucao->descricao $param['descricao'][$row];           
  194.             $solucao->store();
  195.             }
  196.        }
  197.        
  198.            /* $data = new stdClass;
  199.             $data->id = $cliente->id;
  200.             TForm::sendData('form_cliente', $data);*/
  201.             
  202.                
  203.             $object->store(); // stores the object
  204.             
  205.             //busca os dados do evento para exibir
  206.             $object = new Evento_aplicacao($object->id); 
  207.             $object->solucao $object->getSolucaos();
  208.             
  209.             //------------uso do send data-------------------------------
  210.                 $obj = new StdClass;
  211.                 $obj->empreendimento_id $object->empreendimento_id// will fire change action
  212.                 $obj->aplicacao_id $object->aplicacao_id;     // will update the value after the change action
  213.                 
  214.                 // send some data to form dynamically
  215.                 TForm::sendData('form_Evento_aplicacao'$obj);
  216.             //--------------------------------------------
  217.             
  218.             $this->form->setData($object); // keep form data
  219.            // close the transaction
  220.             
  221.             // shows the success message
  222.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  223.             
  224.             TTransaction::close(); 
  225.         
  226.       } catch (Exception $e){ // in case of exception
  227.         
  228.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  229.             //------------uso do send data-------------------------------
  230.                 $obj = new StdClass;
  231.                 $obj->empreendimento_id $object->empreendimento_id// will fire change action
  232.                 $obj->aplicacao_id $object->aplicacao_id;     // will update the value after the change action
  233.                 
  234.                 
  235.                 // send some data to form dynamically
  236.                 TForm::sendData('form_Evento_aplicacao'$obj);
  237.             //--------------------------------------------  
  238.             $this->form->setData$this->form->getData() ); // keep form data
  239.             TTransaction::rollback(); // undo all pending operations
  240.         }
  241.     }
  242.     
  243.     /**
  244.      * method onEdit()
  245.      * Executed whenever the user clicks at the edit button da datagrid
  246.      */
  247.     function onEdit($param)
  248.     {
  249.         try
  250.         {
  251.             if (isset($param['key']))
  252.             {
  253.                 $key=$param['key'];  // get the parameter $key
  254.                 TTransaction::open('sqlserver'); // open a transaction
  255.                 $object = new Evento_aplicacao($key); // instantiates the Active Record
  256.                 
  257.                  //-------------------------------------------
  258.                 $obj = new StdClass;
  259.                 $obj->empreendimento_id $object->empreendimento_id// will fire change action
  260.                 $obj->aplicacao_id $object->aplicacao_id;     // will update the value after the change action
  261.                 
  262.                 $object2 =  Solucao::where('evento_aplicacao_id''='$key)
  263.                                ->orderBy('id')
  264.                                ->load();
  265.                 $object->solucao $object->getSolucaos();
  266.                 
  267.                 
  268.                 foreach ($object2 as $evento_aplicacao
  269.                 {  
  270.                 $this->fieldlist->addDetail($evento_aplicacao);
  271.                 }
  272.                 $this->fieldlist->addCloneAction();
  273.                 // send some data to form dynamically
  274.                 TForm::sendData('form_Evento_aplicacao'$obj);
  275.                 //--------------------------------------------                
  276.                 
  277.                 $this->form->setData($object); // fill the form
  278.                 TTransaction::close(); // close the transaction
  279.             }
  280.             else
  281.             {                
  282.                 $this->fieldlist->addDetail( new stdClass );               
  283.                 $this->form->clear();
  284.             }
  285.         }
  286.         catch (Exception $e// in case of exception
  287.         {
  288.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  289.             TTransaction::rollback(); // undo all pending operations
  290.         }
  291.     }
  292.     //função para carregar as aplicações
  293.     public static function onChange($param)
  294.     {
  295.         TTransaction::open('sqlserver');
  296.         $criteria = new TCriteria;
  297.         $criteria->add(new TFilter('empreendimento_id''='$param['empreendimento_id']));
  298.         
  299.         $repository = new TRepository('Aplicacao');
  300.         $aplicacaos $repository->load($criteria);
  301.         
  302.         $options = array();
  303.         foreach ($aplicacaos as $aplicacao){
  304.             $options[$aplicacao->id] = $aplicacao->sigla;
  305.         }
  306.         
  307.         //TForm::sendData('form_Evento_aplicacao', $options);            
  308.         TCombo::reload('form_Evento_aplicacao''aplicacao_id'$options);
  309.         
  310.         
  311.     }
  312. }
  313. ?>


Qual é o problema ao gravar esse TFieldList? Os dados que vieram do outro componente foi gravado, mas o TFieldList não grava.
Editado 17/07/2023 (há 9 meses) - Clique para ver alterações

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (2)


PD

Eu não vi declarados no form os campos da FieldList.
Veja nesse exemplo:
https://framework.adianti.me/tutor/index.php?class=FormFieldListView

Sem todos esses passos, não irá passar no POST:
  1. <?php
  2. $number = new TEntry('number[]');
  3. $this->fieldlist->addField'<b>Number</b>'$number, ['width' => '25%''sum' => true] );
  4. $this->form->addField($number);
  5. ?>
MA

Consegui solucionar o problema com essa e outras modificações. Muito obrigado.