Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-3635: QueryBuilder - INSTANCE OF with parameter not working #4462

Open
doctrinebot opened this issue Mar 24, 2015 · 8 comments
Open

DDC-3635: QueryBuilder - INSTANCE OF with parameter not working #4462

doctrinebot opened this issue Mar 24, 2015 · 8 comments
Assignees
Labels

Comments

@doctrinebot
Copy link

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.

@doctrinebot
Copy link
Author

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.

@doctrinebot
Copy link
Author

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

@doctrinebot
Copy link
Author

Comment created by @guilhermeblanco:

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

@jycamier
Copy link

jycamier commented Feb 9, 2016

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.

@jarrettj
Copy link

jarrettj commented Jul 6, 2021

Does not work for me @jycamier. I had to put the class name in the parameter. Tried $em->getClassMetadata() no luck. Then played around with it and it works on the class name without the namespace, which is weird. Hey it works :).

@smilesrg
Copy link
Contributor

smilesrg commented Sep 27, 2021

Issue from 24 Mar 2015, is it still relevant?

@guilhermeblanco maybe you know? :-)

@Ocramius
Copy link
Member

@smilesrg would require a test - fairly sure it still doesn't work, since DQL was not refactored in that area.

@jarrettj
Copy link

jarrettj commented Jan 3, 2022

Still does not work. :( Wasn't an issue as we simply had to do the instance check on the result set. Issue now is that the result set has become larger and there will be a performance issue. I had initially thought that the classname worked. But it did not.

Will do some more research and post findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants