Doctrine 条令实体管理器中的Symfony2子查询

Doctrine 条令实体管理器中的Symfony2子查询,doctrine,symfony,entitymanager,query-builder,Doctrine,Symfony,Entitymanager,Query Builder,我需要执行以下查询: SELECT * FROM (SELECT * FROM product WHERE car = 'large' ORDER BY onSale DESC) AS product_ordered GROUP BY type 在Symfony2中使用实体管理器 我的基本查询生成器是: $query = $em->getRepository('AutomotiveBundle:Car') ->createQueryBuilder('p')

我需要执行以下查询:

SELECT * FROM (SELECT * FROM product WHERE car = 'large' ORDER BY onSale DESC) AS product_ordered GROUP BY type
在Symfony2中使用实体管理器

我的基本查询生成器是:

 $query = $em->getRepository('AutomotiveBundle:Car')
        ->createQueryBuilder('p')
        ->where('pr.car = ?1')
        ->andWhere('pr.status = 1')
        ->orderBy('pr.onSale', 'DESC')
        ->setParameter(1, $product->getName())
        ->groupBy('p.type')
        ->getQuery();
但我无法解决如何将子查询添加到此中

我试着做一个单独的查询,然后像这样连接它:

 ->andWhere($query->expr()->in('pr.car = ?1',$query2->getQuery()));
但我得到:

Call to undefined method Doctrine\ORM\Query::expr()

一个技巧是构建两个查询,然后使用getDQL()将第一个查询提供给第二个查询

例如,此查询返回不同的游戏ID列表:

    $qbGameId = $em->createQueryBuilder();

    $qbGameId->addSelect('distinct gameGameId.id');

    $qbGameId->from('ZaysoCoreBundle:Event','gameGameId');

    $qbGameId->leftJoin('gameGameId.teams','gameTeamGameId');

    if ($date1) $qbGameId->andWhere($qbGameId->expr()->gte('gameGameId.date',$date1));
    if ($date2) $qbGameId->andWhere($qbGameId->expr()->lte('gameGameId.date',$date2));
现在使用dql获取有关游戏本身的其他信息:

    $qbGames = $em->createQueryBuilder();

    $qbGames->addSelect('game');
    $qbGames->addSelect('gameTeam');
    $qbGames->addSelect('team');
    $qbGames->addSelect('field');

    $qbGames->addSelect('gamePerson');
    $qbGames->addSelect('person');

    $qbGames->from('ZaysoCoreBundle:Event','game');

    $qbGames->leftJoin('game.teams',   'gameTeam');
    $qbGames->leftJoin('game.persons', 'gamePerson');
    $qbGames->leftJoin('game.field',   'field');

    $qbGames->leftJoin('gameTeam.team',     'team');
    $qbGames->leftJoin('gamePerson.person', 'person');

    // Here is where we feed in the dql
    $qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));

这是一个很长的示例,但我不想编辑内容,可能会破坏它。

您可以使用DBAL执行任何sql查询

$conn = $this->get('database_connection');//create a connection with your DB

$sql="SELECT * FROM (SELECT * FROM product WHERE car =? ORDER BY onSale DESC) AS product_ordered GROUP BY type";   //Your sql Query                
$stmt = $conn->prepare($sql);    // Prepare your sql
$stmt->bindValue(1, 'large');    // bind your values ,if you have to bind another value, you need to write $stmt->bindValue(2, 'anothervalue'); but your order is important so on..
$stmt->execute(); //execute your sql
$result=$stmt->fetchAll(); // fetch your result

快乐编码

努力决定接受谁作为我的答案。实际上,我选择了这条路线,因为它比较小,但它没有使用像Cerads answer这样的查询生成器。Thx asish!顺便说一句,这是给其他还不能熟练使用symfony2的用户的一个小提示。。。如果您已经在存储库中编写了此查询,那么可以通过$conn=$this->_em->getConnection()获得连接;这个解决方案的问题在于它返回的是原始数据而不是实体对象:(我相信这个解决方案忽略了子查询@cerad的限制。例如$qbGameId->setMaxResults(20),当你打印出$qbGames->getDQL()时,你不会看到子查询的限制。一个可能的解决方案: