问题描述
我正在使用CakePHP 3.8并迁移到身份验证插件( https://book.cakephp.org/authentication/1.1/zh-CN/index.html ).
I'm using CakePHP 3.8 and migrating to the Authentication Plugin (https://book.cakephp.org/authentication/1.1/en/index.html).
在控制器中调用 $ this-> Authentication-> getIdentity()-> getOriginalData()
时,我想访问我的 User的几个关联
实体.
When calling $this->Authentication->getIdentity()->getOriginalData()
in a controller, I'd like to access a couple of assocations of my User
entity.
此刻,我正在通过在我的 User
实体中实现以下 IdentityInterface
方法来做到这一点:
At the moment, I'm doing this by implementing the following IdentityInterface
method in my User
entity:
public function getOriginalData() {
$table = TableRegistry::getTableLocator()->get($this->getSource());
$table->loadInto($this, ['Activities', 'Clients']);
return $this;
}
但是我觉得在插件配置中应该有一个 contain
参数(与 AuthComponent
一样).
But I feel there should be a contain
parameter somewhere within the Plugin configuration (as there was with the AuthComponent
).
有人可以在调用 getIdentity()
时指导我如何在用户实体上包括关联吗?
Can anyone guide me on how to include assocations on the User entity when calling getIdentity()
?
推荐答案
旧的Auth组件的身份验证对象的 contain
选项已经过时了,建议的方法是使用自定义查找程序,这也是在新的身份验证插件中完成的操作.
The contain
option of the authentication objects for the old Auth component has been deprecated quite some time ago, and the recommended method is to use a custom finder, and that's also how it's done in the new authentication plugin.
ORM解析器采用 finder
选项,并且必须通过使用的标识符进行配置,在您的情况下,该标识符可能是密码标识符,例如:
The ORM resolver takes a finder
option, and it has to be configured via the used identifier, which in your case is probably the password identifier, ie something like:
$service->loadIdentifier('Authentication.Password', [
// ...
'resolver' => [
'className' => 'Authentication.Orm',
'finder' => 'authenticatedUser' // <<< there it goes
],
]);
然后在您的表类(可能是 UsersTable
)中的finder方法中,您可以包含所需的任何内容:
In the finder method in your table class (probably UsersTable
) you can then contain whatever you need:
public function findAuthenticatedUser(\Cake\ORM\Query $query, array $options)
{
return $query->contain(['Activities', 'Clients']);
}
另请参见
这篇关于CakePHP身份验证插件身份关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!