CakePHP的:得到模型用户信息 [英] CakePHP: get user info in models

查看:127
本文介绍了CakePHP的:得到模型用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一些我发现code内部模型。

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

previously在我的控制器我有

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;
}

于是我打电话AuthComponent从静态的模型。我知道我可以为方法AuthComponent ::密码(),这是非常有用的验证做到这一点。但我使用的方法AuthComponent ::用户()得到错误,特别是

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

致命错误:调用一个成员函数
  检查()的非目的在上
  /var/www/MathOnline/cake/libs/controller/components/auth.php
  线663

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?

推荐答案

有是马特·库里一个很好的解决方案。您当前登录的用户的数据存储在使用beforeFilter回调和访问后使用静态调用app_controller。说明可以在这里找到:
http://www.pseudo$c$cr.com/archives/2008/10/06/accessing-user-sessions-from-models-or-anywhere-in-cakephp-revealed/

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/

编辑:上面的链接已经过时: https://github.com/mcurry/cakephp_static_user

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

这篇关于CakePHP的:得到模型用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆