使用Doctrine2加入条件IN

时间:2014-12-06 01:13:49

标签: sql symfony doctrine-orm doctrine dql

甚至不确定这是否可行,但看起来就像它应该

在我的Video实体中,我为Product实体获得了ManyToOne关联

在这里,我试图获取所有已发布的产品,并通过加入获取属于该产品的视频

// ProductRepository.php
public function findPublished()
{
    $q = $this->getEntityManager()->createQueryBuilder();
    $q->select(['p', 'v'])->from($this->getEntityName(), 'p')
        ->leftJoin('p.videos', 'v', 'ON', 'p IN v.products')
        ->where('p.published = :published')
        ->setParameter('published', true);

    $results = $q->getQuery()->getResult();

    return $results;
}

回来的例外是:

[Syntax Error] line 0, col 76: Error: Expected end of string, got 'ON'
[1/2] QueryException: SELECT p, v FROM Company\CoreBundle\Entity\Product p LEFT JOIN p.videos v ON p IN v.products WHERE p.published = :published

1 个答案:

答案 0 :(得分:0)

如果您的实体映射正确,则无需指定连接条件。试试这个

$q->select(['p', 'v'])->from($this->getEntityName(), 'p')
    ->leftJoin('p.videos', 'v')
    ->where('p.published = :published')
    ->setParameter('published', true);