gpt4 book ai didi

authentication - CakePHP:获取模型中的用户信息

转载 作者:行者123 更新时间:2023-12-04 00:04:18 25 4
gpt4 key购买 nike

我正在模型中移动我的一些查找代码。

以前在我的 Controller 中我有

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

所以在我的评论模型中,我放了类似的东西
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::password() 方法执行此操作,这对验证很有用。但是我在使用 AuthComponent::user() 方法时遇到错误,特别是

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



有没有办法从模型中获取有关当前登录用户的信息?

最佳答案

马特库里有一个很好的解决方案。您使用 beforeFilter 回调将当前登录用户的数据存储在 app_controller 中,然后使用静态调用访问它。可以在此处找到说明:
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

关于authentication - CakePHP:获取模型中的用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2149990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com