Reputation: 69
I am working on a webapp that has three entities: User, Project, and Todo.
Every Todo has a many-to-one relationship with Projects. Todos also have a many-to-many relationship to Users.
What I am trying to do is retrieve projects that contain a todo that has been assigned to a given user.
My code is as follows. $id has been set to the user_id I want to retrieve projects for
$em = $this->getDoctrine()->getEntityManager();
$projects = $em->createQuery("SELECT p FROM projects p INNER JOIN p.todos t WITH t.assigned_to = :id")
->setParameter('id', $id)
->getResult();
Whenever I run this query I get the following error:
[Semantical Error] line 0, col 79 near 'assigned_to =': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
Any thoughts on what I am doing wrong? Thanks.
Upvotes: 4
Views: 9440
Reputation: 16033
Updated:
As the assigned_to relation is a many to many relation pointed out in the comments there is no actual field assigned_to for the comparision.
You have to join the association:
SELECT p
FROM projects p
INNER JOIN p.todos t
INNER JOIN t.assigned_to a
WHERE a.id = :id
I am using the WHERE condition as thats what I am accustomed to, as I learned from you WITH is also supported and might work, too ;)
Upvotes: 6