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???

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)


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

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