Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 576 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cakephp: How to read User Data in Session?

#1
I wanna gather the latest information of a user from the User Model and store it in the session.

so i am doing this

// Before Render
function beforeRender() {

if($this->Session->check('Auth.User')) {
$this->User->recursive = -1;
$currentUser = $this->User->read(null, $this->Session->read('Auth.User.id'));
$this->set(compact('currentUser'));
}
}

It works fine until am in the User model.

When i go to the pages it gives me an error because User->read doesnt work as that model is not attached there.


Whats the best solution for ?

1. I want the latest information of the Loggedin User on every page of the site.
2. Latest and not Auth.User from the session because - when i edit the profile content like name or photo. it still references to the old data that was stored when user had logged on.
3. It has to be displayed on every page like the Stackoverflow User info at the top

![alt text][1]


[1]:



Which is the best way to do it ?
Reply

#2
1. Create `/app/app_controller.php`.
2. In that controller create the `beforeRender` function.
3. Use the usual `$uses`.

See the [book][1].


[1]:

[To see links please register here]

Reply

#3
You can try to load the model in your beforeRender like this: `$this->load('User')`, this should load the model for you in every controller.

[To see links please register here]

Reply

#4
The other suggestions seem to be adding a bit of overhead to your application (by loading and querying the User model for each request). Have you considered simply updating the stale session data when updates occur to your User model?

<?php
class UsersController extends AppController {
public function edit() {
$userId = $this->Session->read('Auth.User.id');
// populate form
if (!$this->data) {
$this->data = $this->User->read($userId, null);
return;
}
// update user
$saved = $this->User->save($this->data);
if ($saved) {
$user = $this->User->read(null, $this->User->id);
$this->Session->write('Auth.User', $user['User']); // update session
$this->Session->setFlash('Account details have been updated');
return $this->redirect(array('action' => 'profile'));
}
$this->Session->setFlash('Please correct the validation errors below');
}
}
?>

This means the data you need will be gathered each request without any additional queries (either from disk if using `php` or `cake` session storage, or in a single session query if using `database` storage).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through