【问题标题】:doctrine to count one to many in query在查询中计算一对多的学说
【发布时间】:2014-01-16 12:44:37
【问题描述】:

我有一个实体Shop,Shop有很多InstagramShopPicture,关系如下:

 /**
     * @Exclude()
     * @ORM\OneToMany(targetEntity="InstagramShopPicture", mappedBy="shop", cascade={"persist"})
     * @ORM\OrderBy({"created" = "DESC"})
     */
    protected $userPictures;

我有以下查询,我需要找到有 4 张或更多图片的商店:

  $query = $em->createQueryBuilder()->select('s')
                  ->from("AppMainBundle:InstagramShop", 's')
                  ->innerJoin('s.userPictures', 'p')
                  ;

      $query->andHaving('COUNT(s.userPictures) >= 4');

为什么这不起作用?正确的做法是什么?

【问题讨论】:

    标签: php symfony orm doctrine-orm doctrine


    【解决方案1】:

    Doctrine 使用 SQL,因此您需要像使用 SQL 一样使用 GROUP BYHAVING

    另外,您需要为COUNT 指定一个字段,例如p.id,而不是别名。

    解释HAVING的链接:http://www.techonthenet.com/sql/having.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多