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

标签 doctrine-orm dql instanceof

我使用 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);

但是,考虑到这个示例反射(reflect)了大型查询的一部分,我想去掉这个join。我试过这个:

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

但它不起作用。

如何避免使用 join?或者也许还有其他方法来优化此类查询?

谢谢

最佳答案

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

可以这样写:

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

或者像这样

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

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

关于doctrine-orm - 原则 2. 查询生成器。关联实例类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31330027/

相关文章:

doctrine-orm - symfony2 : unable to save parent id in embedded object

php - Symfony2 覆盖 User.orm.xml

php - 使用 SUM(a.id=1) 作为 `ìdentifier` 时的学说错误:预期的学说\ORM\查询\Lexer::T_CLOSE_PARENTHESIS,得到 '='

php - 如何在 Symfony 2.8 中的表单存储库中获取用户对象

java - 如何解决 "unreported exception AWTException ; must be caught or declared to be thrown"。机器人实例

php - 更新双向多对多关系的正确方法 symfony2-doctrine

php - 在 Doctrine DBAL 中使用executeQuery 绑定(bind)变量类型时出现问题

php - 学说 dql 没有返回 MySQL 所期望的结果

java - 为什么在 T 是类型参数而 t 是变量的情况下不允许使用 "t instanceof T"?

java - 此方法返回的对象实例是什么?