Last Updated: February 25, 2016
·
6.889K
· hasumedic

Using QueryBuilder and single table inheritance

Here's a quick tip on how to retrieve objects using the discriminator column with the QueryBuilder.

Let's imagine we want to retrieve the employees that holds the chef position in a restaurant. A raw SQL query would look something like:

SELECT * FROM employee WHERE position='chef'

When using Doctrine and single table inheritance, the discriminator column is not in the entity. In order to retrieve the same as the previous query but with the QueryBuilder, we'll need something like:

$em = $this->getEntityManager('db');
$qb = $em->createQueryBuilder();
$qb->select('e')
->from('ProjectMyBundle:Employee', 'e')
->where('e INSTANCE OF ProjectMyBundle:ChefPosition')

The operator INSTANCE OF discriminate whether the employee holds a chef position or not, using the ChefPosition class.

1 Response
Add your response

You know if this work with an interface (I want to be instance of A, B or C, not D, E, and A, B, C are implementing an X Interface)?

Thanks in advance!

over 1 year ago ·