如何使用验证的用户数据检索关联起来呢? [英] How to retrieve associations together with authenticated user data?

查看:167
本文介绍了如何使用验证的用户数据检索关联起来呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表和 UsersProfiles 表 - 两者有明显的关系和用户表存储基本 USER_ID 用户名密码而users_profiles表店 JOB_TITLE 等。

在CakePHP中3,调用验证组件在登录返回基本的用户表行。我想修改相同也返回相应的配置文件一行。我怎样才能做到这一点?

我找到了一种方法来做到这一点 - 但我不知道是否有一个更优雅的或更简单的方法

 公共职能登录(){
        如果($这个 - >请求 - '是('后')){
            $ USER = $这个 - > Auth->确定();
            如果($用户){
                //负荷曲线和准与用户对象
                $剖面= $这个 - >用户 - > UsersProfiles->获取($用户['身份证']);
                $用户['users_profile'] = $的个人资料;
                $这个 - > Auth-> SETUSER($用户);
                返回$这个 - >重定向($这个 - > Auth->配置('loginRedirect'));
            }
            $这个 - > FLASH-GT&;错误(__(无效的用户名或密码,请重试));
        }
    }


解决方案

包含选项

CakePHP的3.1之前,使用包含选项

  $这个 - > loadComponent('验证',[
    '身份验证'=> [
        形= GT; [
            '包含'=> ['UsersProfiles']
        ]
    ]
]);

自定义查找程序

由于3.1可以使用取景器选项来定义取景器用于构建获取用户数据的查询

  $这个 - > loadComponent('验证',[
    '身份验证'=> [
        形= GT; [
            发现者= GT; 权威性
        ]
    ]
]);

在你的表类

 公共职能findAuth(\\蛋糕\\ ORM \\ $查询查询,数组$选项)
{
    返回$查询 - >包含(['UsersProfiles']);
}

这两种解决方案

将确保受 AuthComponent返回的数据::标识()将包含相关的 UsersProfiles

另请参见


  • <一个href=\"http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers\"相对=nofollow>食谱> ...组件>认证>配置身份验证处理程序

  • <一个href=\"http://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query\"相对=nofollow>食谱> ...组件>验证>自定义查找查询

I have a Users table and a UsersProfiles table - the two are obviously related and the user table stores basic user_id, username, password while the users_profiles table stores firstname, lastname, job_title etc.

In CakePHP 3, the call to Authentication Component on login returns the basic user table row. I would like to modify the same to also return the corresponding profile row. How can I do this?

I found a way to do it - but am not sure if there is a more elegant or simpler way.

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

解决方案

The contain option

Before CakePHP 3.1, use the contain option

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'contain' => ['UsersProfiles']
        ]
    ]
]);

A custom finder

As of 3.1 you can use the finder option to define the finder to use for building the query that fetches the user data

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'finder' => 'auth'
        ]
    ]
]);

In your table class

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    return $query->contain(['UsersProfiles']);
}

Both solutions

will ensure that the data returned by AuthComponent::identify() will contain the associated UsersProfiles.

See also

这篇关于如何使用验证的用户数据检索关联起来呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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