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

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto 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