Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Não grava os dados do form Boa noite! Estou iniciando com o Adianti Studio e emperrei em um problema para gravar alguns campos de um formulário criado no FormDesigner; outros campos do mesmo form são salvos, mas os relacionados abaixo não: - dt_aniversario = tipo TDate; - telefone_cel = tipo TEntry; - telefone_fixo = tipo TEntry; - tp_usuarios = tipo TDBCombo; - empresas = tipo TDBCombo; - ativo = tipo TRadioG...
AR
Não grava os dados do form  
Fechado
Boa noite!

Estou iniciando com o Adianti Studio e emperrei em um problema para gravar alguns campos de um formulário criado no FormDesigner; outros campos do mesmo form são salvos, mas os relacionados abaixo não:

- dt_aniversario = tipo TDate;
- telefone_cel = tipo TEntry;
- telefone_fixo = tipo TEntry;
- tp_usuarios = tipo TDBCombo;
- empresas = tipo TDBCombo;
- ativo = tipo TRadioGroup.

No código abaixo verifiquei que em onSave todos os campos, incluindo os citados acima, são lidos em diversas situações (linhas $message comentadas), mas não são gravados. Editei, diretamente no bd, um registro gravado incompleto e executei o onEdit com sucesso, ou seja: recuperou todos os dados que foram gravados (tanto pelo form como pela minha edição direta).

Model:
  1. <?php
  2. class Usuarios extends TRecord
  3. {
  4.     const TABLENAME 'usuarios';
  5.     const PRIMARYKEY'id';
  6.     const IDPOLICY =  'max'// {max, serial}
  7.     private $empresas;
  8.     private $tp_usuarios;
  9.     /**
  10.      * Constructor method
  11.      */
  12.     public function __construct(985 NULL$callObjectLoad TRUE)
  13.     {
  14.         parent::__construct(985$callObjectLoad);
  15.         parent::addAttribute('nome');
  16.         parent::addAttribute('dt_aniversario');
  17.         parent::addAttribute('email');
  18.         parent::addAttribute('login');
  19.         parent::addAttribute('senha');
  20.         parent::addAttribute('telefone_cel');
  21.         parent::addAttribute('telefone_fixo');
  22.         parent::addAttribute('tp_usuarios');
  23.         parent::addAttribute('empresas');
  24.         parent::addAttribute('ativo');
  25.         parent::addAttribute('dt_cadastro');
  26.     }
  27.     
  28.     /**
  29.      * Method set_empresas
  30.      * Sample of usage: $usuarios->empresas = $object;
  31.      * @param $object Instance of Empresas
  32.      */
  33.     public function set_empresas(Empresas $object)
  34.     {
  35.         $this->empresas $object;
  36.         $this->id_empresas $object->id;
  37.     }
  38.     
  39.     /**
  40.      * Method get_empresas
  41.      * Sample of usage: $usuarios->empresas->attribute;
  42.      * @returns Empresas instance
  43.      */
  44.     public function get_empresas()
  45.     {
  46.         // loads the associated object
  47.         if (empty($this->empresas))
  48.             $this->empresas = new Empresas($this->id_empresas);
  49.     
  50.         // returns the associated object
  51.         return $this->empresas;
  52.     }
  53.         
  54.     /**
  55.      * Method set_tp_usuarios
  56.      * Sample of usage: $usuarios->tp_usuarios = $object;
  57.      * @param $object Instance of TpUsuarios
  58.      */
  59.     public function set_tp_usuarios(TpUsuarios $object)
  60.     {
  61.         $this->tp_usuarios $object;
  62.         $this->id_tp_usuarios $object->id;
  63.     }
  64.     
  65.     /**
  66.      * Method get_tp_usuarios
  67.      * Sample of usage: $usuarios->tp_usuarios->attribute;
  68.      * @returns TpUsuarios instance
  69.      */
  70.     public function get_tp_usuarios()
  71.     {
  72.         // loads the associated object
  73.         if (empty($this->tp_usuarios))
  74.             $this->tp_usuarios = new TpUsuarios($this->id_tp_usuarios);
  75.     
  76.         // returns the associated object
  77.         return $this->tp_usuarios;
  78.     }
  79. }
  80. ?>


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

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


FC

Cara confere se o tipo de dados está compatível por exemplo o campo data tem que converter para o formato yyyy/mm/dd se for mysql veja se os outros campos não estão nesse criterio tipo texto em campo numero etc...
AR

Boa tarde, Felipe.

No bd (MySQL) os campos são todos VARCHAR (o mesmo tipo dos que consigo gravar: nome, email, login etc.) e o tipo DATE para dt_aniversario.
FC

Confere então nomes (tem que estar iguais) no banco = model = form da control

Confere senão existe alguma restrição no banco tipo campo maior que o permitido não é nada complicado rapidinho vc acostuma outra coisa da um var_dump abaixo da linha $object = $this->form->getData('Usuarios');

var_dump( $object); de uma checada se os dados estão vindo direitinho como no form ok :)
PD

Além disso, experimente ligar os logs no onSave() para ver os comandos que estão indo para o banco de dados:
www.adianti.com.br/doc-framework-Persistence-Objects-RegisterLog

Att,
Pablo
PD

Além disso Anderson, me permita dar mais uma dica: Uma classe não deve estar no plural, veja por que:
www.adianti.com.br/framework-naming

Att,
Pablo