2016-05-27 13 views
0

Symfonyに2つのテーブルがあり、ManyToOne双方向関係で参加しています。記事と日付。Symfony/Doctrine COUNTグループBy ByとLeft JOIN

私は同じa.idのすべてのd.quantityを合計したいと思います。どうすればできますか?

私が試してみてください。私のテーブルで

(日付)私が持っている:あなたはおそらくHAVINGの代わりに、WHERE、およびa.idによって、ない量のためのグループを使用する必要が

+---------------------------- 
| id | a.id | quantity 
+---------------------------- 
| 1 | 4  |  1 
| 2 | 4  |  3 
| 3 | 6  |  4 
| 4 | 5  |  6 
| 5 | 4  |  15 
---------------------------- 

$allIndispo = $qb 
    ->select('a2.id') 
    ->leftJoin('a2.dates', 'd') 
    ->where('(a2.quantity - COUNT(d.quantity)) <= 0') 
    ->groupBy('d.quantity') 
    ->orderBy('a2.id', 'ASC'); 
+0

を読んでください、あなたはその後、a.idとSUM量によってグループにしたくないですか? –

+0

はい、しかし私がグループをa2.idで変更すると、SQLSTATE [HY000]:一般的なエラー:1111無効な使用dof GROUP – mBbkr

+0

WHERE句はGROUP BY句よりも可能性があります。 http://stackoverflow.com/questions/2330840/mysql-invalid-use-of-group-functionは関連性があります。 –

答えて

1

、およびその結果を(選択して)プロジェクトします。

$allIndispo = $qb 
    ->select('a2.id, SUM(a2.quantity) as total') 
    ->leftJoin('a2.dates', 'd') 
    ->groupBy('a2.id') 
    ->having('(a2.quantity - COUNT(d.quantity)) <= 0') 
    ->orderBy('a2.id', 'ASC'); 
0

あなたはthis

$repository = $this->getDoctrine() 
    ->getRepository('AppBundle:Product'); 

// createQueryBuilder automatically selects FROM AppBundle:Product 
// and aliases it to "p" 
$query = $repository->createQueryBuilder('p') 
    ->where('p.price > :price') 
    ->setParameter('price', '19.99') 
    ->orderBy('p.price', 'ASC') 
    ->getQuery(); 

$products = $query->getResult(); 
// to get just one result: 
// $product = $query->setMaxResults(1)->getOneOrNullResult();