Botao Salvar formulario Studio Prezados Estou utilizando a versão studio criei os botões de editar, salvar no formulário na grid coloco os botões de editar, deletar porem quando vou fazer alguma edição ao gravar ele cria um novo registro deveria alterar apenas, será que alguém pode me dizer onde estou errando segue o código. ...
FC
Botao Salvar formulario Studio  
Fechado
Prezados

Estou utilizando a versão studio criei os botões de editar, salvar no formulário na grid
coloco os botões de editar, deletar porem quando vou fazer alguma edição ao gravar ele cria um novo registro
deveria alterar apenas, será que alguém pode me dizer onde estou errando segue o código.


 
  1. <?php
  2. /**
  3. * UsuariosForm Registration
  4. * @author <your name here>
  5. */
  6. class UsuariosForm extends TPage
  7. {
  8. private $form;
  9. private $datagrid;
  10. private $pageNavigation;
  11. private $loaded;
  12. /**
  13. * Class constructor
  14. * Creates the page and the registration form
  15. */
  16. function __construct()
  17. {
  18. parent::__construct();
  19. // creates the form
  20. $this->form = new TForm('form_Usuarios');
  21. try
  22. {
  23. // TUIBuilder object
  24. $ui = new TUIBuilder(500,500);
  25. $ui->setController($this);
  26. $ui->setForm($this->form);
  27. // reads the xml form
  28. $ui->parseFile('app/forms/FrmUsuarios.form.xml');
  29. // get the interface widgets
  30. $fields = $ui->getWidgets();
  31. // look for the TDataGrid object
  32. foreach ($fields as $name => $field)
  33. {
  34. if ($field instanceof TDataGrid)
  35. {
  36. $this->datagrid = $field;
  37. $this->pageNavigation = $this->datagrid->getPageNavigation();
  38. }
  39. }
  40. // add the TUIBuilder panel inside the TForm object
  41. $this->form->add($ui);
  42. // set form fields from interface fields
  43. $this->form->setFields($ui->getFields());
  44. }
  45. catch (Exception $e)
  46. {
  47. new TMessage('error', $e->getMessage());
  48. }
  49. // add the form to the page
  50. parent::add($this->form);
  51. }
  52. /**
  53. * method onEdit()
  54. * Executed whenever the user clicks at the edit button da datagrid
  55. */
  56. function onEdit($param)
  57. {
  58. try
  59. {
  60. if (isset($param['key']))
  61. {
  62. // get the parameter $key
  63. $key=$param['key'];
  64. // open a transaction with database 'BcoGED'
  65. TTransaction::open('BcoGED');
  66. // instantiates object Usuarios
  67. $object = new Usuarios($key);
  68. // fill the form with the active record data
  69. $this->form->setData($object);
  70. // close the transaction
  71. TTransaction::close();
  72. }
  73. else
  74. {
  75. $this->form->clear();
  76. }
  77. }
  78. catch (Exception $e) // in case of exception
  79. {
  80. // shows the exception error message
  81. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  82. // undo all pending operations
  83. TTransaction::rollback();
  84. }
  85. }
  86. /**
  87. * method onDelete()
  88. * executed whenever the user clicks at the delete button
  89. * Ask if the user really wants to delete the record
  90. */
  91. function onDelete($param)
  92. {
  93. // get the parameter $key
  94. $key=$param['key'];
  95. // define two actions
  96. $action = new TAction(array($this, 'Delete'));
  97. // define the action parameters
  98. $action->setParameter('key', $key);
  99. // shows a dialog to the user
  100. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  101. }
  102. /**
  103. * method Delete()
  104. * Delete a record
  105. */
  106. function Delete($param)
  107. {
  108. try
  109. {
  110. // get the parameter $key
  111. $key=$param['key'];
  112. // open a transaction with database 'BcoGED'
  113. TTransaction::open('BcoGED');
  114. // instantiates object Usuarios
  115. $object = new Usuarios($key);
  116. // deletes the object from the database
  117. $object->delete();
  118. // close the transaction
  119. TTransaction::close();
  120. // reload the listing
  121. $this->onReload();
  122. // shows the success message
  123. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
  124. }
  125. catch (Exception $e) // in case of exception
  126. {
  127. // shows the exception error message
  128. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  129. // undo all pending operations
  130. TTransaction::rollback();
  131. }
  132. }
  133. /**
  134. * method onSave()
  135. * Executed whenever the user clicks at the save button
  136. */
  137. function onSave()
  138. {
  139. try
  140. {
  141. // open a transaction with database 'BcoGED'
  142. TTransaction::open('BcoGED');
  143. // get the form data into an active record Usuarios
  144. $object = $this->form->getData('Usuarios');
  145. // form validation
  146. $this->form->validate();
  147. // stores the object
  148. $object->store();
  149. // set the data back to the form
  150. $this->form->setData($object);
  151. // close the transaction
  152. TTransaction::close();
  153. // shows the success message
  154. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  155. // reload the listing
  156. }
  157. catch (Exception $e) // in case of exception
  158. {
  159. // shows the exception error message
  160. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  161. // undo all pending operations
  162. TTransaction::rollback();
  163. }
  164. }
  165. /**
  166. * method onEdi()
  167. * Executed whenever the user clicks at the edit button da datagrid
  168. */
  169. function onEdi($param)
  170. {
  171. try
  172. {
  173. if (isset($param['key']))
  174. {
  175. // get the parameter $key
  176. $key=$param['key'];
  177. // open a transaction with database 'BcoGED'
  178. TTransaction::open('BcoGED');
  179. // instantiates object Usuarios
  180. $object = new Usuarios($key);
  181. // fill the form with the active record data
  182. $this->form->setData($object);
  183. // close the transaction
  184. TTransaction::close();
  185. }
  186. else
  187. {
  188. $this->form->clear();
  189. }
  190. }
  191. catch (Exception $e) // in case of exception
  192. {
  193. // shows the exception error message
  194. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  195. // undo all pending operations
  196. TTransaction::rollback();
  197. }
  198. }
  199. /**
  200. * method onSave()
  201. * Executed whenever the user clicks at the save button
  202. */
  203. /**
  204. * method onReload()
  205. * Load the datagrid with the database objects
  206. */
  207. function onReload($param = NULL)
  208. {
  209. try
  210. {
  211. // open a transaction with database 'BcoGED'
  212. TTransaction::open('BcoGED');
  213. // creates a repository for Usuarios
  214. $repository = new TRepository('Usuarios');
  215. $limit = 10;
  216. // creates a criteria
  217. $criteria = new TCriteria;
  218. $criteria->setProperties($param); // order, offset
  219. $criteria->setProperty('limit', $limit);
  220. if (TSession::getValue('Usuarios_filter'))
  221. {
  222. // add the filter stored in the session to the criteria
  223. $criteria->add(TSession::getValue('Usuarios_filter'));
  224. }
  225. // load the objects according to criteria
  226. $objects = $repository->load($criteria);
  227. $this->datagrid->clear();
  228. if ($objects)
  229. {
  230. // iterate the collection of active records
  231. foreach ($objects as $object)
  232. {
  233. // add the object inside the datagrid
  234. $this->datagrid->addItem($object);
  235. }
  236. }
  237. // reset the criteria for record count
  238. $criteria->resetProperties();
  239. $count= $repository->count($criteria);
  240. $this->pageNavigation->setCount($count); // count of records
  241. $this->pageNavigation->setProperties($param); // order, page
  242. $this->pageNavigation->setLimit($limit); // limit
  243. // close the transaction
  244. TTransaction::close();
  245. $this->loaded = true;
  246. }
  247. catch (Exception $e) // in case of exception
  248. {
  249. // shows the exception error message
  250. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  251. // undo all pending operations
  252. TTransaction::rollback();
  253. }
  254. }
  255. /**
  256. * method show()
  257. * Shows the page
  258. */
  259. function show()
  260. {
  261. // check if the datagrid is already loaded
  262. if (!$this->loaded)
  263. {
  264. $this->onReload();
  265. }
  266. parent::show();
  267. }
  268. }

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


FC

Eu acredito que vc não está passando o "id" ao salvar, o campo id tem que estar no form senão senão ele entende que não existe e cria um novo registro.
FC

Felipe isso mesmo muito obrigado funcionou !! Porém existe uma forma para esse campo que coloquei no formulário não aparecer para o usuário
FC

Use campo do tipo THidden
FC

Não deu certo ele continua na tela.
PD

Oi Flavio,

Cria o THidden via programação e adiciona no designer:
www.adianti.com.br/forum/pt/view_516?acrescentando-um-thtmleditor-ao

Att,
Pablo