sebastienT
sebastienT

Reputation: 31

Doctrine2 filter query on a child property in class table inheritance

I am trying to filter a Doctrine query made with QueryBuilder on a child property when the object have this property. Basically, I have an abstract class Accommodation with House and Tent as child on a Class table inheritance, as described in the documentation. House class has a owner property and Tent does not. This is how I make my queryBuilder.

$qb = $this->createQueryBuilder('a')
          ->select('a')
          ->andWhere('(a NOT INSTANCE OF "House") OR (a INSTANCE OF "House" AND a.owner = :owner)')
          ->setParameter("owner", $user);

Doing that I get an error saying that owner does not exist for Accommodation :

[Semantical Error] line 0, col 299 near 'owner =:owner)': Error: Class AppBundle\Entity\Accommodation has no field or association named owner

Is there any way to ask Doctrine to join the owner column?

PS: This is part of a Symfony3 project but I don't think it matter for the question.

Upvotes: 2

Views: 1009

Answers (1)

sebastienT
sebastienT

Reputation: 31

Reading those mailing list posts, what I want to do is not possible. So I finally go with 2 queries one one the Accommodation Table with a andWhere(a NOT INSTANCE OF "House") and a second on the House table to get the rest.

That is not really performance optimal but it's the only workaround I see.

If anyone see any other solution, fell free to complete.

Upvotes: 1

Related Questions