Search code examples
doctrine-ormdqlinstanceof

Doctrine 2. query builder. association INSTANCE OF type


I have the following request on DQL, using Doctrine 2 Query Builder, which works well:

$qb->select('post')
    ->from('Posts\Entity\Post','post')
    ->join('post.author','author')
    ->where('author INSTANCE OF :utype')
    ->setParameter('utype',$userType);

But, considering that this example reflects a part of large query, I'd like to get rid of this join. I tried this:

$qb->select('post')
    ->from('Posts\Entity\Post','post')        
    ->where('post.author INSTANCE OF :utype')
    ->setParameter('utype',$userType);

but it doesn't work.

How can I avoid using join? Or maybe there is other ways to optimize such query?

Thank's


Solution

  • I know this is an old question, but the answer is still missing.
    The instance name can not be set as string via parameters.

    It could be written like this:

    $qb->select('post')
        ->from('Posts\Entity\Post','post')        
        ->where('post.author INSTANCE OF '.UserType::class);
    

    or like this

    $qb->select('post')
        ->from('Posts\Entity\Post','post')        
        ->where('post.author INSTANCE OF :utype')
        ->setParameter('utype', $entityManager->getClassMetadata(UserType::class));
    

    @see: https://github.com/doctrine/orm/issues/6483