如何在 Doctrine 中搜索多对多关系?
User:
columns:
id:
type: integer(4)
autoincrement: true
primary: true
username:
type: string(255)
Group:
tableName: group_table
columns:
id:
type: integer(4)
autoincrement: true
primary: true
name:
type: string(255)
relations:
Users:
foreignAlias: Groups
class: User
refClass: GroupUser
GroupUser:
columns:
group_id:
type: integer(4)
primary: true
user_id:
type: integer(4)
primary: true
relations:
Group:
foreignAlias: GroupUsers
User:
foreignAlias: GroupUsers
DB:
USER:
id | username
1 | john
2 | kate
3 | alan
GROUP:
id | name
1 | admin
2 | mod
3 | kate (!)
USERGROUP:
id_user | id_group
1 | 1
2 | 1
1 | 2
3 | 3
3 | 2
2 | 3
我想做搜索系统。我将搜索示例单词:“KATE”。 我如何在多对多表中搜索 KATE?
在输入搜索中我写“KATE”。我必须在 Doctrine 中使用 WHERE LIKE 。 这个查询应该是什么样的? 这应该向我显示用户名 Kate 的所有用户以及 Kate 组的所有用户。
User:
columns:
id:
type: integer(4)
autoincrement: true
primary: true
username:
type: string(255)
Group:
tableName: group_table
columns:
id:
type: integer(4)
autoincrement: true
primary: true
name:
type: string(255)
relations:
Users:
foreignAlias: Groups
class: User
refClass: GroupUser
GroupUser:
columns:
group_id:
type: integer(4)
primary: true
user_id:
type: integer(4)
primary: true
relations:
Group:
foreignAlias: GroupUsers
User:
foreignAlias: GroupUsers
DB:
USER:
id | username
1 | john
2 | kate
3 | alan
GROUP:
id | name
1 | admin
2 | mod
3 | kate (!)
USERGROUP:
id_user | id_group
1 | 1
2 | 1
1 | 2
3 | 3
3 | 2
2 | 3
I would like make search system. I will search for example word: "KATE".
How can i search in many to many table for KATE?
In input search i write "KATE". I must use WHERE LIKE in Doctrine.
How this query must look?
This should show me all user with username Kate and all user for group Kate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 DQL 如下...
搜索名为 kate 的用户或 kate 组中的用户,返回用户和组
FROM User u LEFT JOIN u.Groups g WHERE u.username LIKE 'KATE' OR g.name LIKE “凯特”
所以...
Your DQL ammounts to the following...
Search for Users named kate or Users in the Group kate, returning user and groups
FROM User u LEFT JOIN u.Groups g WHERE u.username LIKE 'KATE' OR g.name LIKE 'KATE'
so...