根据 Kohana 中另一个表的数据对 mysql 结果进行排序
我继承了这个 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>');
因此问题是如何更改查询以便能够按是否允许用户登录进行排序。
I inherited this Kohana project and have little experience with it and ORM.
Table structure is like this:
ROLES TABLE
id
name
ROLES_USERS TABLE
role_id
user_id
USERS TABLE
id
email
password
last_login
The thing is, I need to get users sorted by whether they have a certain role (login in this case) but have no idea how to do that with ORM.
Current query is:
$users = ORM::factory('user')
->limit($pagination->items_per_page)
->offset($pagination->offset)
->order_by('last_login', 'DESC')
->find_all();
and then when outputting it's printed like this:
$row['status'][] = ($user->has('roles', ORM::factory('role', array('name' => 'login')))
? '<span class="green">Active</span>'
: '<span class="red">Blocked</span>');
So the question would be how to alter the query to be able to sort by whether users are allowed to login or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望您没有 2000 个角色,但活跃用户应该首先使用此查询
I hope you don't have 2000 roles, but active users should come first using this query
也许您可以获取登录角色的用户?
Maybe you could just get the users for the login role?