Symfony2 / Doctrine多个连接返回错误 [英] Symfony2 / Doctrine multiple joins returns error
问题描述
表:消息
- id
-
- 文本
表:作业
- id
- 注释
- 状态
- id
- assignment_id
- user_id
- id
- 名称
表:Assignment_User
表: / strong>
现在我想检索特定用户的所有分配的消息
$ qb- > select('DISTINCT m')
- > from('MessageBundle:Assignment','a')
- > join('MessageBundle:Message','m')
- > join('MessageBundle:AssignmentUser','au')
- > where('a.message = m')
- > andWhere('au.assignment = a'
- > andWhere('a.status =(:assigned)')
- & setParameter('assigned','assigned')
- > orderBy(mr.createdAt,desc);
一旦添加第二个JOIN,它会抛出一个错误...
错误:预期的文字,得到'JOIN'
获取用户X的所有分配的消息的正确方法是什么?
原则应该已经了解了您的表与您的实体映射的关系(如果不是,您可能需要先设置它们)。而不是尝试加入整个表,然后指定表的连接(SQL风格),只需将 au.assignment
加入 a
。
此时,您还可以在连接上指定任何其他条件,例如您想在 a.status
。
您可能希望根据您在哪些方向设置了哪些关联来重新排列连接(我不知道您的消息
具有分配
属性,例如)。
('MessageBundle:AssignmentUser','au')
- > innerJoin('au.assignment ','a','WITH','a.status =((assign)')
- > innerJoin('au.user','u')
- > innerJoin
- > where('u.id =(:user_id)')
- > setParameter('assigned','assigned')
- > setParameter('user_id',$ yourSpecificUserId)
- > orderBy(m.createdAt,desc);
I'm building a system with messages that can be assigned to (one or more) users.
Table: Messages
- id
- text
Table: Assignments
- id
- remark
- status
Table: Assignment_User
- id
- assignment_id
- user_id
Table: Users
- id
- name
Now I want to retrieve all assigned messages for a specific user
$qb->select('DISTINCT m')
->from('MessageBundle:Assignment', 'a')
->join('MessageBundle:Message', 'm')
->join('MessageBundle:AssignmentUser', 'au')
->where('a.message = m')
->andWhere('au.assignment = a')
->andWhere('a.status = (:assigned)')
->setParameter('assigned', 'assigned')
->orderBy("mr.createdAt", "desc");
As soon as I add that second JOIN it throws an error... Error: Expected Literal, got 'JOIN'
What would be the correct way to get all assigned messages for user X?
Doctrine should already understand how your tables relate through your entity mappings (if not, you might need to set those up first). Rather than trying to join a whole table and then specifying what the table is joined on (SQL-style), just join, for example, au.assignment
as a
.
At that point, you can also specify any additional conditions on your join, such as you'd want on a.status
.
You might prefer to reorder the joins below depending on what associations you have set up in which directions (I don't know whether your Message
has an assignments
property, for example).
$qb->select('DISTINCT m')
->from('MessageBundle:AssignmentUser', 'au')
->innerJoin('au.assignment', 'a', 'WITH', 'a.status = (:assigned)')
->innerJoin('au.user', 'u')
->innerJoin('a.message', 'm')
->where('u.id = (:user_id)')
->setParameter('assigned', 'assigned')
->setParameter('user_id', $yourSpecificUserId)
->orderBy("m.createdAt", "desc");
这篇关于Symfony2 / Doctrine多个连接返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!