0

对于 cakephp 3.6,cakephp.org 告诉如何在以下链接中自定义身份验证组件的用户查找器查询: link 但我不知道如何实现它?我在属于部门表的用户表中有'department_id'列。我想更改以下查询:

public function findAuth(){
    $query
    ->select(['id', 'username', 'password'])->contain(['Departments'])
        ->where(['Users.active' => 1]);
    return $query;
}

上面的代码会起作用吗?请告诉我我必须在哪个文件中编写函数?还有什么其他必要的步骤来完成它,以便我在 $this->Auth->User 中获取所有用户相关信息?

4

1 回答 1

0

首先,您需要加载 Auth 组件并在您想要使用自定义查找器的任何控制器中传递自定义配置,如下所示:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'finder' => 'auth'
            ]
        ],
    ]);
}

然后,在您的中,UsersTable您将拥有自定义查找器:

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    $query
    ->select(['id', 'username', 'password'])->contain(['Departments'])
    ->where(['Users.active' => 1]);

    return $query;
}

这个答案也可能有助于在自定义身份验证查找器中包含表

于 2018-09-27T09:59:35.770 回答