Cakephp 2.6:如何从 cakephp Auth 中的关联 table 获取角色

Cakephp 2.6: How to get role from associative table in cakephp Auth

我有一个table名字users,和user_types有关系。用户类型 table 具有字段名称 role。我需要 Auth 组件的这个角色字段。所以我试过

$type = $this->Auth->user(['Usertype']['name']);

我在这里收到通知名称未定义。我可以获得此数据添加另一个查询并将其设置在会话中。我的问题是,是否可以通过 cakephp Auth 组件获取它?

就像我们可以通过下面的代码获取 Auth 用户名

$this->Auth->user('username'); 

关联数据也可以吗?

只需使用

$type = $this->Auth->user('Usertype.name');

您正确命名了 table,但不是模型。 user_types table 对应的模型是 UserType。如果你设置了正确的关联(这意味着你的 users table 应该有像 user_type_id 这样命名的字段),你应该这样得到它:

  $this->Auth->user('UserType.name');

更多关于命名的信息 here