CakePHP身份验证插件身份关联

问题描述 投票:0回答:1

我正在使用CakePHP 3.8并迁移到身份验证插件(https://book.cakephp.org/authentication/1.1/en/index.html)。

[在控制器中调用$this->Authentication->getIdentity()->getOriginalData()时,我想访问我的User实体的几个关联。

此刻,我正在通过在IdentityInterface实体中实现以下User方法来做到这一点:

public function getOriginalData() {
  $table = TableRegistry::getTableLocator()->get($this->getSource());
  $table->loadInto($this, ['Activities', 'Clients']);
  return $this;
}

但是我觉得在插件配置中应该有一个contain参数(就像AuthComponent一样。

有人可以在引导getIdentity()时指导我如何在用户实体上包括关联吗?

authentication cakephp associations cakephp-3.x
1个回答
0
投票

很早以前不推荐使用旧的Auth组件的身份验证对象的contain选项,推荐的方法是使用自定义查找程序,这也是在新的身份验证插件中完成的方式。

ORM解析器采用finder选项,并且必须通过使用的标识符进行配置,在您的情况下,该标识符可能是密码标识符,例如:

$service->loadIdentifier('Authentication.Password', [
    // ...
    'resolver' => [
        'className' => 'Authentication.Orm',
        'finder' => 'authenticatedUser' // <<< there it goes
    ],
]);

然后在表类的finder方法(可能是UsersTable中,您可以包含所需的任何内容:

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

另请参见

© www.soinside.com 2019 - 2024. All rights reserved.