In a catalog, I have products and articles. Articles are variants of products. In a catalog, products are sorted in categories and a product can be one or many times in a catalog.
I'd like to get articles of a catalog, but my articles aren't assigned directly to the catalog, only products are.
I'd like to construct the following SQL using Doctrine's query builder:
SELECT a.code, a.productCode, a.name
FROM Article a
INNER JOIN (
SELECT p.code
FROM Product p
WHERE p.catalogCode = 'MYCODE'
GROUP BY p.code
ORDER BY p.code ASC
) AS results ON results.productCode = a.productCode
This query works in MySQL. I tried to do it in the Repository of my entity, but I have an error :
public function findArticlesByCatalog($catatlogCode)
{
return $this->getEntityManager()
->createQuery(
'SELECT a.code, a.productCode, a.name
FROM AppBundle:Article a
INNER JOIN (
SELECT p.code
FROM AppBundle:CatalogProduct p
WHERE p.catalogCode = :code
GROUP BY p.code
ORDER BY p.code ASC
) AS results ON results.productCode = a.productCode'
)
->setParameter('code', $catatlogCode)
->getResult();
}
Error (just after INNER JOIN) :
[Semantical Error] line 0, col 81 near '(
SELECT': Error: Class '(' is not defined.
So, I'd like to construct it using Doctrine's query builder in my Controller.
I started something but I don't know to finish it...
$repository = $em->getRepository('AppBundle:Article');
$qb = $repository->createQueryBuilder('a');
$qb->select(array('a.code', 'a.productCode', 'a.name'))
->innerJoin(
'AppBundle:CatalogProduct', 'p',
'WITH',
$qb->select('p.code')
->where(
$qb->expr()->eq('p.catalogCode', ':code')
)
->setParameter('code', $catCode)
->groupBy('p.code')
->orderBy('p.code', 'ASC')
)
// ...
How to specify the rest of the query ?
AS results ON results.productCode = a.productCode'
Thanks for your help !