Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Dúvida de salvamento em lote Bom dia pessoal... preciso alterar objetos em lote como o exemplo do tutor ProductUpdateList (http://adianti.com.br/framework_files/tutor/index.php?class=ProductUpdateList) ; porém n meu caso eu crio dois objetos, então quando eu capturo os dados com o "$field->getValue()" os dois assumem o mesmo valor, o que é lógico pq eu não estou sabendo como capturar os valor individuais. Pergunta como ...
JF
Dúvida de salvamento em lote  
Bom dia pessoal... preciso alterar objetos em lote como o exemplo do tutor ProductUpdateList (adianti.com.br/framework_files/tutor/index.php?class=ProductUpdateLi) ; porém n meu caso eu crio dois objetos, então quando eu capturo os dados com o "$field->getValue()" os dois assumem o mesmo valor, o que é lógico pq eu não estou sabendo como capturar os valor individuais. Pergunta como pega os dados individualmente tipo: $data = $this->formgrid->getFields(); $nome = $data->nome...ou algo parecido, alguém sabe como fazer ?
estou salvando como no exemplo do tutor abaixo.
Obrigado

  1. <?php
  2. public function onSaveCollection()
  3.     {
  4.         $data $this->formgrid->getData(); // get datagrid form data
  5.         $this->formgrid->setData($data); // keep the form filled
  6.         
  7.         try
  8.         {
  9.             // open transaction
  10.             TTransaction::open('permission');
  11.             
  12.             // iterate datagrid form objects
  13.             foreach ($this->formgrid->getFields() as $name => $field)
  14.             {
  15.                 if ($field instanceof TCheckButton)
  16.                 {
  17.                     $parts explode('_'$name);
  18.                     $id end($parts);
  19.                     
  20.                     $object SystemUserGroupProgram::find($id);
  21.                     if ($object)
  22.                     {
  23.                         //$object->acesso   =  $field->acesso; 
  24.                         $object->acesso   =  $field->getValue(); 
  25.                                     
  26.                         $object->store();
  27.                     }
  28.                     
  29.                 }
  30.             }
  31.             new TMessage('info'AdiantiCoreTranslator::translate('Records updated'));
  32.             
  33.             // close transaction
  34.             TTransaction::close();
  35.         }
  36.         catch (Exception $e)
  37.         {
  38.             // show the exception message
  39.             new TMessage('error'$e->getMessage());
  40.         }
  41.         
  42.     }//onSaveCollection
  43. ?>


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 (4)


JF

Pra ser mais claro, é uma tela de permissão onde eu tenho que capturar por checkbox o acesso, permissão e deleção... porém quando eu salvo os dados ele salva todos os valores iguais ao deleção que é o último que é salvo. Vou postar o código todo

  1. <?php
  2. class testeDataGrid3 extends TPage
  3. {
  4.     protected $form;     // registration form
  5.     protected $datagrid// listing
  6.     protected $pageNavigation;
  7.     protected $formgrid;
  8.     protected $saveButton;
  9.     
  10.     // trait with onReload, onSearch, onDelete...
  11.     use Adianti\Base\AdiantiStandardListTrait;
  12.     
  13.     /**
  14.      * Page constructor
  15.      */
  16.     public function __construct()
  17.     {
  18.         parent::__construct();
  19.         
  20.         $this->setDatabase('permission');
  21.         $this->setActiveRecord('SystemUserGroupProgram');
  22.         $this->setDefaultOrder('id''asc');
  23.         $this->addFilterField('system_program_id''=''system_program_id');
  24.         
  25.         // creates the form
  26.         $this->form = new BootstrapFormBuilder('form_search_Product');
  27.         $this->form->setFormTitle(_t('Batch update list'));
  28.         
  29.         // create the form fields
  30.         $system_program_id = new TEntry('system_program_id');
  31.         $this->form->addFields( [new TLabel('Pesquisa por ID')], [$system_program_id] );//description
  32.         
  33.         $this->form->addAction(_t('Find'), new TAction(array($this'onSearch')), 'fa:search');
  34.         
  35.         // keep the form filled with session data
  36.         $this->form->setDataTSession::getValue('ProductUpdateList_filter_data') );
  37.         
  38.         // creates the datagrid
  39.         $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  40.         $this->datagrid->disableDefaultClick();
  41.         $this->datagrid->style 'width: 100%';
  42.         
  43.         // create the datagrid columns
  44.         $column_id             = new TDataGridColumn('id''Id''left''20%');
  45.         $column_system_prog_id = new TDataGridColumn('system_program_id''Id Programa''left''20%');
  46.         $column_acesso         = new TDataGridColumn('acesso_widget''Acesso''center''20%');
  47.         $column_insercao       = new TDataGridColumn('insercao_widget''Insercao''right''20%');
  48.         $column_delecao        = new TDataGridColumn('delecao_widget''Deleçao''right''20%');
  49.         
  50.         $this->datagrid->addColumn($column_id);
  51.         $this->datagrid->addColumn($column_system_prog_id);
  52.         $this->datagrid->addColumn($column_acesso);
  53.         $this->datagrid->addColumn($column_insercao);
  54.         $this->datagrid->addColumn($column_delecao);
  55.         
  56.         // create the datagrid model
  57.         $this->datagrid->createModel();
  58.         
  59.         // creates the pagination
  60.         $this->pageNavigation = new TPageNavigation;
  61.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  62.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  63.         
  64.         // put datagrid inside a form
  65.         $this->formgrid = new TForm;
  66.         $this->formgrid->add($this->datagrid);
  67.         
  68.         // creates the update collection button
  69.         $this->saveButton = new TButton('update_collection');
  70.         $this->saveButton->setAction(new TAction(array($this'onSaveCollection')), 'Save');
  71.         $this->saveButton->setImage('fa:save green');
  72.         $this->formgrid->addField($this->saveButton);
  73.         
  74.         $gridpack = new TVBox;
  75.         $gridpack->style 'width: 100%';
  76.         $gridpack->add($this->formgrid);
  77.         $gridpack->add($this->saveButton)->style 'background:whiteSmoke;border:1px solid #cccccc; padding: 3px;padding: 5px;';
  78.         
  79.         // define the datagrid transformer method
  80.         $this->setTransformer(array($this'onBeforeLoad'));
  81.         
  82.         // vertical box container
  83.         $container = new TVBox;
  84.         $container->style 'width: 100%';
  85.         $container->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  86.         $container->add($this->form);
  87.         $container->add($panel TPanelGroup::pack(''$gridpack$this->pageNavigation));
  88.         $panel->getBody()->style 'overflow-x: auto';
  89.         parent::add($container);
  90.     }
  91.     
  92.     /**
  93.      * Run before the datagrid is loaded
  94.      */
  95.     public function onBeforeLoad($objects$param)
  96.     {
  97.         // update the action parameters to pass the current page to action
  98.         // without this, the action will only work for the first page
  99.         $saveAction $this->saveButton->getAction();
  100.         $saveAction->setParameters($param); // important!
  101.         
  102.         $gridfields = array( $this->saveButton );
  103.         
  104.         foreach ($objects as $object)
  105.         {
  106.             $object->acesso_widget = new TCheckButton('acesso' '_' $object->id);
  107.             $object->acesso_widget->setValue$object->acesso );
  108.         $object->acesso_widget->setIndexValue('1');
  109.             $gridfields[] = $object->acesso_widget;// important
  110.             
  111.         $object->insercao_widget = new TCheckButton('insercao' '_' $object->id);
  112.             $object->insercao_widget->setValue$object->insercao );
  113.         $object->insercao_widget->setIndexValue('1');
  114.             $gridfields[] = $object->insercao_widget// important
  115.             
  116.         $object->delecao_widget = new TCheckButton('delecao' '_' $object->id);
  117.             $object->delecao_widget->setValue$object->delecao );
  118.         $object->delecao_widget->setIndexValue('1');
  119.             $gridfields[] = $object->delecao_widget;// important
  120.             
  121.         }
  122.         
  123.         $this->formgrid->setFields($gridfields);
  124.         
  125.     }//onBeforeLoad
  126.     
  127.     /**
  128.      * Save the datagrid objects
  129.      */
  130.     public function onSaveCollection()
  131.     {
  132.         $data $this->formgrid->getData(); // get datagrid form data
  133.         $this->formgrid->setData($data); // keep the form filled
  134.         
  135.         try
  136.         {
  137.             // open transaction
  138.             TTransaction::open('permission');
  139.             
  140.             // iterate datagrid form objects
  141.             foreach ($this->formgrid->getFields() as $name => $field)
  142.             {
  143.                 if ($field instanceof TCheckButton)
  144.                 {
  145.                     $parts explode('_'$name);
  146.                     $id end($parts);
  147.                     
  148.                     $object SystemUserGroupProgram::find($id);
  149.                     if ($object)
  150.                     {
  151.                         $object->acesso   =  $field->getValue(); 
  152.                         $object->insercao =  $field->getValue(); 
  153.                         $object->delecao  =  $field->getValue();
  154.                                     
  155.                         $object->store();
  156.                     }
  157.                     
  158.                 }
  159.             }
  160.             new TMessage('info'AdiantiCoreTranslator::translate('Records updated'));
  161.             
  162.             // close transaction
  163.             TTransaction::close();
  164.         }
  165.         catch (Exception $e)
  166.         {
  167.             // show the exception message
  168.             new TMessage('error'$e->getMessage());
  169.         }
  170.         
  171.     }//onSaveCollection
  172.     
  173. }
  174. ?>
NR

A função $field->getValue() retorna o valor do campo atual dentro do foreach. Como você está atribuindo esse valor aos 3 atributos do objeto, todos terão sempre o valor do último campo. Adicione um if dentro do foreach para passar somente 1 vez por "linha" e depois pegue o valor dos outros 2 campos via getData:
  1. <?php
  2. if ($field instanceof TCheckButton)
  3. {
  4.      $parts explode('_'$name);
  5.      $id end($parts);
  6.      $nome_campo $parts[0];
  7.                     
  8.      if ($nome_campo == 'acesso')
  9.      {
  10.            $object SystemGroupProgram::find($id);
  11.            if ($object)
  12.            {
  13.                  $object->acesso   =  $field->getValue(); 
  14.                  $object->insercao =  $data->{"insercao_$id"}; 
  15.                  $object->delecao  =  $data->{"delecao_$id"};
  16.                                     
  17.                  $object->store();
  18.            }
  19.       }
  20. }
  21. ?>
JF

Obrigado mesmo Nataniel deu certo agora...grande abraço.
JF

Estou fazendo umas alterações nessa tela e vi a necessidade de ter algumas TEntry porém só consigo capturar os valores do Formgrid, os do form quando tento capturar com $this->form->getData() trás os dados vazios. como faço ?