获取关联模型与$ this-> Auth在Cakephp [英] Getting associated models with $this->Auth in Cakephp

查看:155
本文介绍了获取关联模型与$ this-> Auth在Cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP 2.0的集成Auth组件。
我有以下表格:

I am using CakePHP 2.0's integrated Auth component. I have the following tables :



  • 用户

  • Groups

  • 个人资料

如下:

User belongsTo Group
User hasMany Profiles

登录到网站时,我注意到Auth会话只包含用户表信息,但是对于已登录的用户,我也需要Groups和Profiles表的信息。

While logged in to the site, I noticed the Auth session contains only User table information, but I need the information of Groups and Profiles tables too for the logged in user.

有什么方法可以用Auth组件吗?

Is there any way to do that with the Auth component?

推荐答案

p>没有办法用 AuthComponent 这样做,因为它处理会话密钥的方式。

There is no way to do this with the AuthComponent because of the way it handles the session keys. You can, however, just save it to the session yourself.

这样做的唯一方法是在用户登录时添加到会话中:

The only way to do this is to add to the session when the user logs in:

function login() {
    if ($this->Auth->login($this->data)) {
        $this->User->id = $this->Auth->user('id');
        $this->User->contain(array('Profile', 'Group'));
        $this->Session->write('User', $this->User->read());
    }
}

然后在 beforeFilter ),在您的 AppController 中,为控制器保存一个var以获取:

Then in your beforeFilter() in your AppController, save a var for the controllers to get to:

function beforeFilter() {
    $this->activeUser = $this->Session->read('User');
}

// and allow the views to have access to user data
function beforeRender() {
    $this->set('activeUser', $this->activeUser);
}

更新:从CakePHP 2.2 href =http://bakery.cakephp.org/articles/lorenzo/2012/07/01/cakephp_2_2_and_2_1_4_released =nofollow>在此处公布),AuthComponent现在接受用于存储的contains键会话中的额外信息。

Update: As of CakePHP 2.2 (announced here), the AuthComponent now accepts the 'contain' key for storing extra information in the session.

这篇关于获取关联模型与$ this-> Auth在Cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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