Authentication 如何与经过身份验证的用户数据一起检索关联?

Authentication 如何与经过身份验证的用户数据一起检索关联?,authentication,cakephp,associations,cakephp-3.0,Authentication,Cakephp,Associations,Cakephp 3.0,我有一个Users表和一个UsersProfiles表-这两个表显然是相关的,用户表存储基本的userid,username,password,而Users\u profiles表存储firstname,lastname,job\u title等 在CakePHP3中,登录时对身份验证组件的调用返回基本用户表行。我想对其进行修改,以同时返回相应的配置文件行。我该怎么做 我找到了一种方法,但不确定是否有更优雅或更简单的方法 public function login() { if

我有一个
Users
表和一个
UsersProfiles
表-这两个表显然是相关的,用户表存储基本的
userid
username
password
,而Users\u profiles表存储
firstname
lastname
job\u title

在CakePHP3中,登录时对身份验证组件的调用返回基本用户表行。我想对其进行修改,以同时返回相应的配置文件行。我该怎么做

我找到了一种方法,但不确定是否有更优雅或更简单的方法

public function login() {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                // load profile and associate with user object
                $profile = $this->Users->UsersProfiles->get($user['id']);
                $user['users_profile'] = $profile;
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->config('loginRedirect'));
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }
包含
选项 在CakePHP3.1之前,请使用
contain
选项

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'contain' => ['UsersProfiles']
        ]
    ]
]);
自定义查找器 从3.1开始,您可以使用
finder
选项定义用于构建获取用户数据的查询的finder

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'finder' => 'auth'
        ]
    ]
]);
在你的桌子课上

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    return $query->contain(['UsersProfiles']);
}
两种解决方案 将确保由
AuthComponent::identify()
返回的数据将包含关联的
UsersProfiles

另见

您好,我也有同样的问题,但您的解决方案没有付费,如何从UsersProfiles表恢复数据?@IsaacPalacio我不知道您到底想说什么,但答案中显示的示例确实有效。如果您在实现这一点时遇到问题,那么您可能需要打开一个新问题,在这里您可以正确地详细说明您所面临的具体问题,显示您的代码,等等。您好,很抱歉给您带来不便,但我已经解决了它,但不包含,但使用join,也非常感谢