doctrine 2查询构建器计算一对多关系

时间:2013-09-07 03:41:11

标签: php symfony doctrine-orm query-builder

我有以下查询:

  $em = $this->getEntityManager();
        $query = $em->createQueryBuilder()->select('shoppingcart')
            ->from("AppMainBundle:ShoppingCart", 'shoppingcart')
            ->innerJoin('shoppingcart.shoppingCartProducts', 'shoppingcartproduct')
            ->innerJoin('shoppingcartproduct.product', 'product')
            ->innerJoin('shoppingcartproduct.productAttribute', 'productattribute')
            ->innerJoin('product.shop', 'shop')
            ;

如何编写where语句,我只想获得包含多个shoppingcartproduct的shoppingcart?这是购物车和购物车产品的关系:

class ShoppingCart
{
    /**
     * @var integer $id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;



    /**
     * @ORM\OneToMany(targetEntity="ShoppingCartProduct", mappedBy="shoppingCart", cascade={"persist","remove"})
     */
    protected $shoppingCartProducts;
}

1 个答案:

答案 0 :(得分:2)

尝试添加以下行

->addSelect('COUNT(shoppingcartproduct) as nProducts')
->addGroupBy('shoppingcart.id')
->having('nProducts > 1')