phppostgresqldoctrine-ormdoctrinedoctrine-query

Doctrine QueryBuilder can't parse WHERE X IN EntityField


When I try to select all entities where a number is in a Collection, Doctrine throws an Exception:

[Syntax Error] line 0, col 70: Error: Expected =, <, <=, <>, >, >=, !=, got 'IN'

The QueryBuilder is used this way:

$this->getEntityManager()->createQueryBuilder()->select('f0')->from(Factory::class, 'f0')->where("${companyId} IN f0.offices");

Facotry::offices is a ManyToMany relationship.

What am I doing wrong?


Solution

  • The ORM Magic does not work like I would have expected. You can't use the ManyToMany Field as an array and the parser expects a simple comparison if the left part is an integer. To get the expected result you have to join on the Field and filter this result on the id.

    $qb->innerJoin("f0.offices", 'be')->where("be.id = ${companyId}");