Inclusão de dados pelo Formulário Bom dia, Não consigo gravar dados quando enviado através do formulário. Veja, anexo, video....
ST
Inclusão de dados pelo Formulário  
Bom dia,
Não consigo gravar dados quando enviado através do formulário.
Veja, anexo, video.

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


FC

Posta o código
ST

Segue código do modelo:


 
  1. <?php
  2. /**
  3. * Cliente Active Record
  4. * @author <your-name-here>
  5. */
  6. class Cliente extends TRecord
  7. {
  8. const TABLENAME = 'cliente';
  9. const PRIMARYKEY= 'ID';
  10. const IDPOLICY = 'max'; // {max, serial}
  11. private $cidades;
  12. /**
  13. * Constructor method
  14. */
  15. public function __construct($id = NULL, $callObjectLoad = TRUE)
  16. {
  17. parent::__construct($id, $callObjectLoad);
  18. parent::addAttribute('nome');
  19. parent::addAttribute('cidade_id');
  20. }
  21. /**
  22. * Method addCidade
  23. * Add a Cidade to the Cliente
  24. * @param $object Instance of Cidade
  25. */
  26. public function addCidade(Cidade $object)
  27. {
  28. $this->cidades[] = $object;
  29. }
  30. /**
  31. * Method getCidades
  32. * Return the Cliente' Cidade's
  33. * @return Collection of Cidade
  34. */
  35. public function getCidades()
  36. {
  37. return $this->cidades;
  38. }
  39. /**
  40. * Reset aggregates
  41. */
  42. public function clearParts()
  43. {
  44. $this->cidades = array();
  45. }
  46. /**
  47. * Load the object and its aggregates
  48. * @param $id object ID
  49. */
  50. public function load($id)
  51. {
  52. // load the related Cidade objects
  53. $repository = new TRepository('Cidade');
  54. $criteria = new TCriteria;
  55. $criteria->add(new TFilter('cidade_id', '=', $id));
  56. $this->cidades = $repository->load($criteria);
  57. // load the object itself
  58. return parent::load($id);
  59. }
  60. /**
  61. * Store the object and its aggregates
  62. */
  63. public function store()
  64. {
  65. // store the object itself
  66. parent::store();
  67. // delete the related Cidade objects
  68. $criteria = new TCriteria;
  69. $criteria->add(new TFilter('cidade_id', '=', $this->id));
  70. $repository = new TRepository('Cidade');
  71. $repository->delete($criteria);
  72. // store the related Cidade objects
  73. if ($this->cidades)
  74. {
  75. foreach ($this->cidades as $cidade)
  76. {
  77. unset($cidade->id);
  78. $cidade->cidade_id = $this->id;
  79. $cidade->store();
  80. }
  81. }
  82. }
  83. /**
  84. * Delete the object and its aggregates
  85. * @param $id object ID
  86. */
  87. public function delete($id = NULL)
  88. {
  89. $id = isset($id) ? $id : $this->id;
  90. // delete the related Cidade objects
  91. $repository = new TRepository('Cidade');
  92. $criteria = new TCriteria;
  93. $criteria->add(new TFilter('cidade_id', '=', $id));
  94. $repository->delete($criteria);
  95. // delete the object itself
  96. parent::delete($id);
  97. }
  98. }
  99. Segue código do formulário:
 
  1. <?php
  2. /**
  3. * ClienteForm Registration
  4. * @author <your name here>
  5. */
  6. class ClienteForm 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_Cliente');
  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/clientes.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 onSave()
  54. * Executed whenever the user clicks at the save button
  55. */
  56. function onSave()
  57. {
  58. try
  59. {
  60. // open a transaction with database 'teste'
  61. TTransaction::open('teste');
  62. // get the form data into an active record Cliente
  63. $object = $this->form->getData('Cliente');
  64. // form validation
  65. $this->form->validate();
  66. // stores the object
  67. $object->store();
  68. // set the data back to the form
  69. $this->form->setData($object);
  70. // close the transaction
  71. TTransaction::close();
  72. // shows the success message
  73. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  74. // reload the listing
  75. }
  76. catch (Exception $e) // in case of exception
  77. {
  78. // shows the exception error message
  79. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  80. // undo all pending operations
  81. TTransaction::rollback();
  82. }
  83. }
  84. /**
  85. * method onEdit()
  86. * Executed whenever the user clicks at the edit button da datagrid
  87. */
  88. function onEdit($param)
  89. {
  90. try
  91. {
  92. if (isset($param['key']))
  93. {
  94. // get the parameter $key
  95. $key=$param['key'];
  96. // open a transaction with database 'teste'
  97. TTransaction::open('teste');
  98. // instantiates object Cliente
  99. $object = new Cliente($key);
  100. // fill the form with the active record data
  101. $this->form->setData($object);
  102. // close the transaction
  103. TTransaction::close();
  104. }
  105. else
  106. {
  107. $this->form->clear();
  108. }
  109. }
  110. catch (Exception $e) // in case of exception
  111. {
  112. // shows the exception error message
  113. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  114. // undo all pending operations
  115. TTransaction::rollback();
  116. }
  117. }
  118. }</your>
ST