原则 2. 查询生成器。关联实例类型

Vik*_*tor 2 instanceof dql doctrine-orm

我使用 Doctrine 2 Query Builder 对 DQL 有以下请求,效果很好:

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

但是,考虑到这个例子反映了大型查询的一部分,我想摆脱这个join。我试过这个:

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

但它不起作用。

我怎样才能避免使用join?或者也许还有其他方法来优化此类查询?

谢谢

vir*_*ize 5

我知道这是一个老问题,但仍然缺少答案。
实例名称不能通过参数设置为字符串。

可以这样写:

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

或者像这样

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

@参见: https: //github.com/doctrine/orm/issues/6483