【问题标题】:kohana orm select users with all given roleskohana orm 选择具有所有给定角色的用户
【发布时间】:2014-09-05 13:33:10
【问题描述】:

我需要选择具有所有给定角色的用户。 在 Model_User 我有:

 public function getUsers() {

   $users = $this
            ->join('roles_users')->on('roles_users.user_id', '=', 'user.id')
            ->where('role_id', '=', ORM::factory('Role', array('name' => 'login')))
            ->and_where('role_id', '=', ORM::factory('Role', array('name' => 'simple_user')));

    return $users;
}

但这不起作用。 问候。

【问题讨论】:

标签: php select orm kohana-3


【解决方案1】:

如果您关注 Ryan 发布的链接,您可以通过这种方式修改该脚本:

public function get users() {

    $login = ORM::factory('role', array('name' => 'login'))->users->find_all()->as_array();
    $simple_user = ORM::factory('role', array('name' => 'simple_user'))->users->find_all()->as_array();

    return array_intersect($login, $simple_user);
}

因此,您应该只有同时拥有这两个角色的用户。

另一件小事是您应该在函数名称中遵循 kohana 约定。

【讨论】:

    【解决方案2】:

    我会为这个问题建议一种不同的方法:

    public function getUsers() {
        // Get IDs of all users having "login" role
        $active_users = ORM::factory('Role', array('name' => 'login'))->users->find_all()->as_array(null, 'id');
        // Select all users having roles "simple user" and "login"
        $users = ORM::factory('Role', array('name' => 'simple_user'))->users->where('id', 'IN', $active_users)->find_all();
        return $users;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2021-10-06
      • 2021-04-11
      相关资源
      最近更新 更多