DQL子查询问题

时间:2017-05-01 21:21:24

标签: php mysql symfony doctrine-orm doctrine

我试图从表o中切换*,其中供应商是我传递的。(这是在Doctrine存储库的上下文中)。然后我想运行一个子查询和SELECT * FROM AppBundle:PriceOption,其中p.offero。我在运行此代码时遇到了QueryException:

public function getVendorFeaturedDeals(Vendor $vendor){
    $purchaseOptions = $this->
        getEntityManager()
        ->createQueryBuilder()
        ->from('AppBundle:PriceOption', 'p')
            ->innerJoin('p.offer', 'o')
        ->getDQL();


    $query = $this->createQueryBuilder('o');
    return $query
        ->where('o.vendor = :vendor')
        ->addSelect(sprintf('(%s)', $purchaseOptions))
        ->setParameter(':vendor', $vendor)
        ->getQuery()
        ->execute();
}

出现错误:AppBundle\Tests\Service\VendorServiceTest::testGetVendorFeaturedDeals Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 18: Error: Unexpected 'FROM'Doctrine\ORM\Query\QueryException: SELECT o, (SELECT FROM AppBundle:PriceOption p INNER JOIN p.offer o) FROM AppBundle\Entity\Offer o WHERE o.vendor = :vendor

引起

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

您应该将查询修改为:

@Test
public void test_2() throws Exception {
    Firm catering = DS.firm().get(1);
    LocalDateTime ldtFrom = LocalDateTime.of(2017, Month.JANUARY, 1, 0, 0);
    LocalDateTime ldtTo = LocalDateTime.of(2017, Month.MAY, 2, 0, 0);
    Date dFrom = Date.from(ldtFrom.atZone(ZoneId.systemDefault()).toInstant());
    Date dTo = Date.from(ldtTo.atZone(ZoneId.systemDefault()).toInstant());

    List<PersonOrders> LON = DS.firm().getAllOrders(catering, dFrom, dTo, false);
    Map<Object, Long> M = LON.stream().collect(
            Collectors.groupingBy(p
                    -> Arrays.asList(p.getDatum(), p.getPerson().getIdfirm(), p.getIdProduct()),
                    Collectors.counting()));

    for (Map.Entry<Object, Long> e : M.entrySet()) {
        Object key = e.getKey();
        Long value = e.getValue();
        System.err.println(String.format("Client firm :%s, total: %d", key, value));
    }
}