Mysql Cakephp 3 findAuth联接表返回数组
我正在尝试使用自定义查找器实现基于角色的身份验证系统Mysql Cakephp 3 findAuth联接表返回数组,mysql,arrays,cakephp,role-base-authorization,Mysql,Arrays,Cakephp,Role Base Authorization,我正在尝试使用自定义查找器实现基于角色的身份验证系统 public function findAuth(\Cake\ORM\Query $query, array $options) { $query ->select(['id', 'username', 'passwordHash', 'locked', 'roles.role']) ->group('username') ->join([ 'table' => 'use
public function findAuth(\Cake\ORM\Query $query, array $options)
{
$query
->select(['id', 'username', 'passwordHash', 'locked', 'roles.role'])
->group('username')
->join([
'table' => 'user_roles',
'conditions' => ['user_roles.userid = Users.id']])
->join([
'table' => 'roles',
'conditions' => ['roles.id = user_roles.role']])
->toArray()
;
return $query;
}
我需要的结果mysql查询是:
选择users.id、username、passwordHash、locked、group_concat(roles.role)role from users internal JOIN user_roles on user_roles.userid=users.id internal JOIN roles on roles.id=user_roles.role group by users.id我最终得到的结果是:
public function findAuth(\Cake\ORM\Query $query, array $options)
{
$query
->select(['id', 'username', 'passwordHash', 'locked'])
->join([
'table' => 'user_roles',
'conditions' => ['user_roles.user_id = Users.id']])
->contain(['UserRoles.Roles'])
->join([
'table' => 'roles',
'conditions' => ['roles.id = user_roles.role']])
->group(['Users.username']);
return $query;
}
这给了我一个多维数组:
用户角色[索引[id,用户id,角色[id,角色]]
在我的例子中,我有两个索引项(0和1),我可以使用foreach循环中的in_数组函数检查用户的角色你的意思是只需要返回查询吗?我需要使用查询生成器生成我在上面发布的MySQL查询,以选择包含一个用户和多个角色的一行:[1][John][$2$10…][false]['admin','tech','manager']您应该返回查询对象,不要对查询对象调用->toArray()