3

I have the following query:

 $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') ; 

how do I write a where statement, where I only want to get shoppingcart that has more than one shoppingcartproduct in it? Here's the relationship of shopping cart and shoppingcart product:

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 Answer 1

3

Try to add following lines

->addSelect('COUNT(shoppingcartproduct) as nProducts') ->addGroupBy('shoppingcart.id') ->having('nProducts > 1') 
Sign up to request clarification or add additional context in comments.

3 Comments

How can I access "nProducts" from result of query? here I use getResult() on query object.
$result[0] will be Shoppingcart, $result['nProducts'] = 123;
In case the query should return an array of objects and the actual number of products does not matter, the select clause should be COUNT(shoppingcardproduct) AS HIDDEN nProducts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.