我正在使用 CakePHP 3.8 并迁移到身份验证插件 ( https://book.cakephp.org/authentication/1.1/en/index.html )。
在 Controller 中调用 $this->Authentication->getIdentity()->getOriginalData()
时,我想访问我的 User
的几个关联> 实体。
目前,我通过在我的 User
实体中实现以下 IdentityInterface
方法来实现此目的:
public function getOriginalData() {
$table = TableRegistry::getTableLocator()->get($this->getSource());
$table->loadInto($this, ['Activities', 'Clients']);
return $this;
}
但我觉得插件配置中的某处应该有一个 contain
参数(就像 AuthComponent
一样)。
任何人都可以指导我如何在调用 getIdentity()
时在 User 实体上包含关联吗?
最佳答案
旧 Auth 组件的身份验证对象的 contain
选项早已被弃用,推荐的方法是使用自定义查找器,新版本中也是这样做的身份验证插件。
ORM 解析器采用 finder
选项,并且必须通过使用的标识符进行配置,在您的情况下,该标识符可能是密码标识符,即类似:
$service->loadIdentifier('Authentication.Password', [
// ...
'resolver' => [
'className' => 'Authentication.Orm',
'finder' => 'authenticatedUser' // <<< there it goes
],
]);
在表类(可能是 UsersTable
)的 finder 方法中,您可以包含您需要的任何内容:
public function findAuthenticatedUser(\Cake\ORM\Query $query, array $options)
{
return $query->contain(['Activities', 'Clients']);
}
另请参阅
关于authentication - CakePHP 身份验证插件身份关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58640337/