使用 Doctrine ORM DQL (NOT IN) 排除行
我正在使用 codeigniter 和原则构建一个聊天应用程序。
表格:
- 用户
- 用户角色
- User_available
关系:
一个用户有多个角色。 ONE user_available 拥有 1 个用户。
可聊天的用户将位于 user_available 表中。
问题:
我需要让 user_available 中没有 role_id 7 的所有用户加入。
所以我需要在 DQL 中表达类似的内容(这甚至不是 SQL,只是用文字表达):
SELECT * from user_available WHERE NOT user_available.User.Role.role_id = 7
真的坚持这一
编辑: 猜猜我不清楚。表已经映射,Doctrine 为我完成了 INNER JOIN 工作。 我正在使用此代码来获取等待时间最长的管理员,但现在我需要用户:
$admin = Doctrine_Query::create()
->select('c.id')
->from('Chat_available c')
->where('c.User.Roles.role_id = ?', 7)
->groupBy('c.id')
->orderBy('c.created_at ASC')
->fetchOne();
现在我需要获取等待时间最长的用户,但这不起作用
$admin = Doctrine_Query::create()
->select('c.id')
->from('Chat_available c')
->where('c.User.Roles.role_id != ?', 7)
->groupBy('c.id')
->orderBy('c.created_at ASC')
->fetchOne();
I'm building a chat application with codeigniter and doctrine.
Tables:
- User
- User_roles
- User_available
Relations:
ONE user have MANY roles.
ONE user_available have ONE user.
Users available for chatting will be in the user_available table.
Problem:
I need to get all users in in user_available that hasn't got role_id 7.
So I need to express in DQL something like (this is not even SQL, just in words):
SELECT * from user_available WHERE NOT user_available.User.Role.role_id = 7
Really stuck on this one
EDIT:
Guess I was unclear. The tables are already mapped and Doctrine does the INNER JOIN job for me.
I'm using this code to get the admin that waited the longest but now I need the user:
$admin = Doctrine_Query::create()
->select('c.id')
->from('Chat_available c')
->where('c.User.Roles.role_id = ?', 7)
->groupBy('c.id')
->orderBy('c.created_at ASC')
->fetchOne();
Now I need to get the user that waited the longest but this does NOT work
$admin = Doctrine_Query::create()
->select('c.id')
->from('Chat_available c')
->where('c.User.Roles.role_id != ?', 7)
->groupBy('c.id')
->orderBy('c.created_at ASC')
->fetchOne();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这个问题现在可能已经消失了,但这就是我为未来的读者做的事情:
I realise the problem has probably gone away by now, but this is how I do it for future readers:
为此,您需要首先连接表
使用内部连接将 user_available.id 连接到 user.id
但是,我认为您可能错误地使用了 user_available 表。既然和user表是1对1匹配的,那你的user表就不能有一个is_available字段吗?
To do this you need to JOIN the tables first
Use an inner join to join user_available.id to user.id
However, I think you may be using the user_available table incorrectly. As it is matched 1 to 1 with the user table, can't you just have an is_available field in your user table?