Lançado Adianti Framework 7.6!
Clique aqui para saber mais
registrar o conteúdo da sessão de login no bando de dados Galera tenho um formulário de entrada de dados, criado no stúdio Pro, mas não estou conseguindo o código pra registrar o usuário que está realizando o cadastro no banco de dados... preciso pegar o login do usuário que está logado e inseri-lo no banco de dados o mesmo com a data de inserção... alguém pode me ajudar???...
CC
registrar o conteúdo da sessão de login no bando de dados  
Fechado
Galera tenho um formulário de entrada de dados, criado no stúdio Pro, mas não estou conseguindo o código pra registrar o usuário que está realizando o cadastro no banco de dados... preciso pegar o login do usuário que está logado e inseri-lo no banco de dados o mesmo com a data de inserção... alguém pode me ajudar???

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)


MG

Cleiton

Você deve ter utilizado o exemplo de login usado nos exemplos certo?

Verifique se no controle que está sendo feito login se existe algo do tipo "TSession::setValue('login',$login)?

Se tiver é só usar no seu controle algo como "$login = TSession::getValue('login').

Se não tiver, você mesmo pode criar.

Espero ter ajudado!

Abraços
Marcelo
CC

Marcelo, é isso mesmo deu certinho, muito obrigado...
CC

Marcelo, o código para exibir o usuário logado está correto, com ele exibo em uma página em branco o nome de quem está logado, mas não estou conseguindo exibir esta informação no objeto TEntry... do formulário e muito menos salva-lo no banco de dados quando salvo o registro.

segue o código do formulário:

  1. <?php
  2. /**
  3.  * departamentoFormList Registration
  4.  * @author  <your name here>
  5.  */
  6. class departamentoFormList extends TPage
  7. {
  8.     protected $form// form
  9.     protected $datagrid// datagrid
  10.     protected $pageNavigation;
  11.     protected $loaded;
  12.     
  13.     /**
  14.      * Class constructor
  15.      * Creates the page and the registration form
  16.      */
  17.     function __construct()
  18.     {
  19.         parent::__construct();
  20.         $login TSession::getValue('username');
  21.         
  22.         // creates the form
  23.         $this->form = new TQuickForm('form_departamento');
  24.         $this->form->class 'tform'// CSS class
  25.         $this->form->setFormTitle('departamento'); // define the form title
  26.         
  27.         // create the form fields
  28.         $depDepartamento                = new TEntry('depDepartamento');
  29.         $depLider                       = new TEntry('depLider');
  30.         $depUsuario                     = new TEntry('depUsuario');
  31.         // add the fields
  32.         $this->form->addQuickField('depDepartamento'$depDepartamento,  200);
  33.         $this->form->addQuickField('depLider'$depLider,  200);
  34.         $this->form->addQuickField('depUsuario'$depUsuario,  200);
  35.         // create the form actions
  36.         $this->form->addQuickAction(_t('Save'), new TAction(array($this'onSave')), 'ico_save.png');
  37.         $this->form->addQuickAction(_t('New'),  new TAction(array($this'onEdit')), 'ico_new.png');
  38.         
  39.         // creates a DataGrid
  40.         $this->datagrid = new TQuickGrid;
  41.         $this->datagrid->setHeight(320);
  42.         
  43.         // creates the datagrid columns
  44.         $depDepartamento $this->datagrid->addQuickColumn('depDepartamento''depDepartamento''left'200);
  45.         $depLider $this->datagrid->addQuickColumn('depLider''depLider''left'200);
  46.         $depUsuario $this->datagrid->addQuickColumn('depUsuario''depUsuario''left'200);
  47.         
  48.         // create the datagrid actions
  49.         $edit_action   = new TDataGridAction(array($this'onEdit'));
  50.         $delete_action = new TDataGridAction(array($this'onDelete'));
  51.         
  52.         // add the actions to the datagrid
  53.         $this->datagrid->addQuickAction(_t('Edit'), $edit_action'id''ico_edit.png');
  54.         $this->datagrid->addQuickAction(_t('Delete'), $delete_action'id''ico_delete.png');
  55.         
  56.         // create the datagrid model
  57.         $this->datagrid->createModel();
  58.         
  59.         // creates the page navigation
  60.         $this->pageNavigation = new TPageNavigation;
  61.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  62.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  63.         
  64.         // create the datagrid model
  65.         $this->datagrid->createModel();
  66.         
  67.         // creates the page navigation
  68.         $this->pageNavigation = new TPageNavigation;
  69.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  70.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  71.         
  72.         // create the page container
  73.         $container TVBox::pack$this->form$this->datagrid$this->pageNavigation);
  74.         parent::add($container);
  75.     }
  76.     /**
  77.      * method onReload()
  78.      * Load the datagrid with the database objects
  79.      */
  80.     function onReload($param NULL)
  81.     {
  82.         try
  83.         {
  84.             // open a transaction with database 'conectmmv'
  85.             TTransaction::open('conectmmv');
  86.             
  87.             // creates a repository for departamento
  88.             $repository = new TRepository('departamento');
  89.             $limit 10;
  90.             // creates a criteria
  91.             $criteria = new TCriteria;
  92.             
  93.             // default order
  94.             if (empty($param['order']))
  95.             {
  96.                 $param['order'] = 'id';
  97.                 $param['direction'] = 'asc';
  98.             }
  99.             $criteria->setProperties($param); // order, offset
  100.             $criteria->setProperty('limit'$limit);
  101.             
  102.             if (TSession::getValue('departamento_filter'))
  103.             {
  104.                 // add the filter stored in the session to the criteria
  105.                 $criteria->add(TSession::getValue('departamento_filter'));
  106.             }
  107.             
  108.             // load the objects according to criteria
  109.             $objects $repository->load($criteriaFALSE);
  110.             
  111.             $this->datagrid->clear();
  112.             if ($objects)
  113.             {
  114.                 // iterate the collection of active records
  115.                 foreach ($objects as $object)
  116.                 {
  117.                     // add the object inside the datagrid
  118.                     $this->datagrid->addItem($object);
  119.                 }
  120.             }
  121.             
  122.             // reset the criteria for record count
  123.             $criteria->resetProperties();
  124.             $count$repository->count($criteria);
  125.             
  126.             $this->pageNavigation->setCount($count); // count of records
  127.             $this->pageNavigation->setProperties($param); // order, page
  128.             $this->pageNavigation->setLimit($limit); // limit
  129.             
  130.             // close the transaction
  131.             TTransaction::close();
  132.             $this->loaded true;
  133.         }
  134.         catch (Exception $e// in case of exception
  135.         {
  136.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  137.             TTransaction::rollback(); // undo all pending operations
  138.         }
  139.     }
  140.     
  141.     /**
  142.      * method onDelete()
  143.      * executed whenever the user clicks at the delete button
  144.      * Ask if the user really wants to delete the record
  145.      */
  146.     function onDelete($param)
  147.     {
  148.         // define the delete action
  149.         $action = new TAction(array($this'Delete'));
  150.         $action->setParameters($param); // pass the key parameter ahead
  151.         
  152.         // shows a dialog to the user
  153.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  154.     }
  155.     
  156.     /**
  157.      * method Delete()
  158.      * Delete a record
  159.      */
  160.     function Delete($param)
  161.     {
  162.         try
  163.         {
  164.             // get the parameter $key
  165.             $key=$param['key'];
  166.             
  167.             TTransaction::open('conectmmv'); // open the transaction
  168.             $object = new departamento($keyFALSE); // instantiates the Active Record
  169.             $object->delete(); // deletes the object
  170.             TTransaction::close(); // close the transaction
  171.             
  172.             $this->onReload$param ); // reload the listing
  173.             new TMessage('info'TAdiantiCoreTranslator::translate('Record deleted')); // success message
  174.         }
  175.         catch (Exception $e// in case of exception
  176.         {
  177.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  178.             TTransaction::rollback(); // undo all pending operations
  179.         }
  180.     }
  181.     
  182.     /**
  183.      * method onSave()
  184.      * Executed whenever the user clicks at the save button
  185.      */
  186.     function onSave()
  187.     {
  188.         try
  189.         {
  190.             TTransaction::open('conectmmv'); // open a transaction with database
  191.             
  192.             // get the form data into an active record departamento
  193.             $object $this->form->getData('departamento');
  194.             $this->form->validate(); // form validation
  195.             $object->store(); // stores the object
  196.             $this->form->setData($object); // fill the form with the active record data
  197.             TTransaction::close(); // close the transaction
  198.             
  199.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved')); // success message
  200.             $this->onReload(); // reload the listing
  201.             $this->form->clear();
  202.         }
  203.         catch (Exception $e// in case of exception
  204.         {
  205.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  206.             TTransaction::rollback(); // undo all pending operations
  207.         }
  208.     }
  209.     
  210.     /**
  211.      * method onEdit()
  212.      * Executed whenever the user clicks at the edit button da datagrid
  213.      */
  214.     function onEdit($param)
  215.     {
  216.         try
  217.         {
  218.             if (isset($param['key']))
  219.             {
  220.                 
  221.                 $key=$param['key']; // get the parameter $key
  222.                 TTransaction::open('conectmmv'); // open a transaction with the database
  223.                 $object = new departamento($key); // instantiates the Active Record
  224.                 $this->form->setData($object); // fill the form with the active record data
  225.                 TTransaction::close(); // close the transaction
  226.             }
  227.             else
  228.             {
  229.                 $this->form->clear();
  230.             }
  231.         }
  232.         catch (Exception $e// in case of exception
  233.         {
  234.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  235.             TTransaction::rollback(); // undo all pending operations
  236.         }
  237.     }
  238.     
  239.     /**
  240.      * method show()
  241.      * Shows the page e seu conteúdo
  242.      */
  243.     function show()
  244.     {
  245.         // check if the datagrid is already loaded
  246.         if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  247.         {
  248.             $this->onReloadfunc_get_arg(0) );
  249.         }
  250.         parent::show();
  251.     }
  252. }
  253. ?>

se alguém puder mu ajudar ficarei muito grato...</your>
MG

Cleiton

Para colocar um valor num TEntry geralmente usamos a sintaxe $objentry->setValue($valor) ou adicionando os valores em onReload.

Em onReload você está chamando TCriteria adicionando TSession::getValue('departamento_filter').

Qual o conteúdo de "departamento_filter"? É um TFilter()?

PD

Como o Marcelo comentou,

Faltou algo tipo:
$depUsuario->setValue( TSession::getValue('login') );

Embora eu não colocaria o usuário logado na tela, pois permitiria que alguém fosse lá e alterasse para outra pessoa. Se você precisa gravar o usuário logado, simplesmente faça isso no onSave() de maneira transparente.

Att,
Pablo