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. try
  7. {
  8. // open transaction
  9. TTransaction::open('permission');
  10. // iterate datagrid form objects
  11. foreach ($this->formgrid->getFields() as $name => $field)
  12. {
  13. if ($field instanceof TCheckButton)
  14. {
  15. $parts = explode('_', $name);
  16. $id = end($parts);
  17. $object = SystemUserGroupProgram::find($id);
  18. if ($object)
  19. {
  20. //$object->acesso = $field->acesso;
  21. $object->acesso = $field->getValue();
  22. $object->store();
  23. }
  24. }
  25. }
  26. new TMessage('info', AdiantiCoreTranslator::translate('Records updated'));
  27. // close transaction
  28. TTransaction::close();
  29. }
  30. catch (Exception $e)
  31. {
  32. // show the exception message
  33. new TMessage('error', $e->getMessage());
  34. }
  35. }//onSaveCollection
  36. ?>


Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


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