具有多个连接的cakephp查询

时间:2012-03-20 11:25:59

标签: mysql cakephp cakephp-2.0

我想知道是否有更好的方法来实现这一目标:

$rights = $this->User->Role->Right->query(
    "SELECT ri.*
    FROM rights AS ri 
    LEFT JOIN roles_rights AS rr ON rr.right_id = ri.id 
    LEFT JOIN users_roles AS ur ON ur.role_id = rr.role_id
    WHERE ur.user_id = " . Sanitize::clean($user['User']['id']));

我希望,有更好的方法可以获得特定用户的所有权利。

我想通过$ this-> User->访问权限......这可能吗?

用户拥有一个或多个角色,角色拥有一项或多项权利。

1 个答案:

答案 0 :(得分:0)

尝试翻过来:

$rights = $this->User->Role->Right->query(
  SELECT ri.* 
  FROM users_roles AS ur
  INNER JOIN roles_rights AS rr ON rr.role_id = ur.role_id
  INNER JOIN rights ri ON ri.id = rr.right_id
  WHERE ur.user_id = Sanitize::clean($user['User']['id']));