Orm: DDC-3635: QueryBuilder - INSTANCE OF with parameter not working

Created on 24 Mar 2015  路  4Comments  路  Source: doctrine/orm

Jira issue originally created by user Wilt:

I use single table class inheritance.
When I write my INSTANCE OF* clause in my *QueryBuilder like this:

    $result = $this->createQueryBuilder('o')
        ->leftJoin('o.child', 'c')
        ->where('c INSTANCE OF :entity_class')
        ->setParameter('entity_class', 'My\Entity\Class')
        ->getQuery()
        ->getResult();

It does not work correctly. The problem seems to lie in the fact that the parameter is bound to the SQL query. So if I change the entity_class to the discriminator column name it works correctly.

So if the discriminator value for My\Entity\Class* would be *discriminator then this does work:

    $result = $this->createQueryBuilder('o')
        ->leftJoin('o.child', 'c')
        ->where('c INSTANCE OF :entity_class')
        ->setParameter('entity_class', 'discriminator')
        ->getQuery()
        ->getResult();

So the step for getting the discriminator column from the class name is missing and instead the class name is bound to the MySql query directly.

Bug

Most helpful comment

Comment created by @guilhermeblanco:

INSTANCE OF expressions expect either their class names or their corresponding class metadata. This means the following examples are valid:

$qb->where('c INSTANCE OF My\Entity\Class');

// or

$qb
    ->where('c INSTANCE OF :param')
    ->setParameter('param', $em->getClassMetadata('My\Entity\Class'))
;

Assigning class name directly does not work.
Closing as invalid.

All 4 comments

Comment created by @guilhermeblanco:

INSTANCE OF expressions expect either their class names or their corresponding class metadata. This means the following examples are valid:

$qb->where('c INSTANCE OF My\Entity\Class');

// or

$qb
    ->where('c INSTANCE OF :param')
    ->setParameter('param', $em->getClassMetadata('My\Entity\Class'))
;

Assigning class name directly does not work.
Closing as invalid.

Comment created by @ocramius:

Re-opening. I think this is a documentation issue then, as http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#id2 says that the InstanceOfParameter accepts also an InputParameter

Comment created by @guilhermeblanco:

[~ocramius] And it does. However, the bound parameter must be a ClassMetadata.

It seams that there is a similar issue on the DoctrineORMAdapter object.

    $query = $this->createQueryBuilder('o')
        ->leftJoin('o.child', 'c')
        ->where('c INSTANCE OF :class')
        ->setParameter('class',  $em->getClassMetadata('My\Entity\Class'))
        ->getQuery();
    $adapter = new DoctrineORMAdapter($query);
    ...

This main query is okay : the mapping on the discriminator is done with the discriminator value. However, the subquery create to get the count isn't abble to do the job : the mapping is done with the string 'My\Entity\Class' and not the discriminator value.

Was this page helpful?
0 / 5 - 0 ratings