【问题标题】:Doctrine 2. query builder. association INSTANCE OF type学说 2. 查询生成器。关联实例类型
【发布时间】:2015-07-09 23:27:09
【问题描述】:

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

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

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

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

但它不起作用。

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

谢谢

【问题讨论】:

  • 会发生什么错误?
  • 预期的 Doctrine\ORM\Query\Lexer::T_INSTANCE,得到 '.'靠近 post.author
  • posts 和 author 是 2 个不同的表。帖子中的加入字段将只是一个 ID。所以你必须加入。
  • 也许有一些方法可以访问鉴别器列?然而,它也涉及到 join..
  • 无论如何,我发现了性能问题的原因。这不是关于连接的,它是由另一部分查询中的连接语句中的 OR 条件引起的:$qb->join($userType,'u','WITH','post.destination_member_id=u.id OR post.author_id=u.id');

标签: doctrine-orm dql instanceof


【解决方案1】:

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

可以这样写:

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2013-09-29
    • 2011-06-01
    • 2012-12-07
    • 2016-04-02
    相关资源
    最近更新 更多