CakePHP: get user info in models

2019-03-13 01:26发布

I'm moving some of my find code inside models.

Previously in my controller I had

$this->Book->Review->find('first', array(
    'conditions' => array(
        'Review.book_id' => $id,
        'Review.user_id' => $this->Auth->user('id')
    )
));

so in my Review model I put something like

function own($id) {
    $this->contain();
    $review = $this->find('first', array(
        'conditions' => array(
            'Review.book_id' => $id,
            'Review.user_id' => AuthComponent::user('id')
        )
    ));
    return $review;
}

So I'm calling AuthComponent statically from the Model. I know I can do this for the method AuthComponent::password(), which is useful for validation. But I'm getting errors using the method AuthComponent::user(), in particular

Fatal error: Call to a member function check() on a non-object in /var/www/MathOnline/cake/libs/controller/components/auth.php on line 663

Is there a way to get the info about the currently logged user from a model?

9条回答
趁早两清
2楼-- · 2019-03-13 01:46

The way that I use is this:

App::import('component', 'CakeSession');        
$thisUserID = CakeSession::read('Auth.User.id');

It seems to work quite nicely :-)

查看更多
我只想做你的唯一
3楼-- · 2019-03-13 01:51

I think this is not good idea to get value from Session. Better solution to get logged user id inside any model simply try this:

AuthComponent::user('id');

This will work almost every where. View, Model and Controller

查看更多
男人必须洒脱
4楼-- · 2019-03-13 01:52

I think the code is fine as it is and belongs in the Controller, or at the very least it needs to receive the ids from the Controller and not try to get them itself. The Model should only be concerned with fetching data from a data store and returning it. It must not be concerned with how the data is handled in the rest of the application or where the parameters to its request are coming from. Otherwise you paint yourself into a corner where the ReviewModel can only retrieve data for logged in users, which might not always be what you want.

As such, I'd use a function signature like this:

function findByBookAndUserId($book_id, $user_id) {
    …
}

$this->Review->findByBookAndUserId($id, $this->Auth->user('id'));
查看更多
倾城 Initia
5楼-- · 2019-03-13 01:55

I use cake 2.2 and these both work great:

$this->Session->read('Auth.User');
//or
$this->Auth->user();

You can also get a field of currently logged in user:

$this->Session->read('Auth.User.email');
//or
$this->Auth->user()['email'];
查看更多
孤傲高冷的网名
6楼-- · 2019-03-13 02:01

There is a nice solution by Matt Curry. You store the data of the current logged user in the app_controller using the beforeFilter callback and access it later using static calls. A description can be found here: http://www.pseudocoder.com/archives/2008/10/06/accessing-user-sessions-from-models-or-anywhere-in-cakephp-revealed/


EDIT: the above link is outdated: https://github.com/mcurry/cakephp_static_user

查看更多
太酷不给撩
7楼-- · 2019-03-13 02:02

Create a new function in the "app_model.php" ("AppModel.php" in CakePHP 2.x), so it will be available at all models within our application:

function getCurrentUser() {
  // for CakePHP 1.x:
  App::import('Component','Session');
  $Session = new SessionComponent();

  // for CakePHP 2.x:
  App::uses('CakeSession', 'Model/Datasource');
  $Session = new CakeSession();


  $user = $Session->read('Auth.User');

  return $user;
}

in the model:

$user = $this->getCurrentUser();
$user_id = $user['id'];
$username = $user['username'];
查看更多
登录 后发表回答