我继承了这个 Kohana 项目,对它和 ORM 几乎没有经验。
表结构是这样的:
ROLES TABLE
id
name
ROLES_USERS TABLE
role_id
user_id
USERS TABLE
id
email
password
last_login
问题是,我需要根据用户是否具有特定角色(在本例中为登录)对用户进行排序,但不知道如何使用 ORM 来做到这一点。
当前查询是:
$users = ORM::factory('user')
->limit($pagination->items_per_page)
->offset($pagination->offset)
->order_by('last_login', 'DESC')
->find_all();
然后在输出时打印如下:
$row['status'][] = ($user->has('roles', ORM::factory('role', array('name' => 'login')))
? '<span class="green">Active</span>'
: '<span class="red">Blocked</span>');
所以问题是如何改变查询,以便能够按用户是否被允许登录进行排序。