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.
Written by Alex
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Doctrine2
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#