我有这个SQL查询。
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
COUNT(bsc.id) AS business_count
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN (
fi_business b
INNER JOIN
fi_business_subcategory bsc ON b.id = bsc.business_id AND (bsc.deleted_at IS NULL)
INNER JOIN
fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1
) ON sc.id = bsc.subcategory_id
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
因为DQL不支持此类查询。我想将它与Doctrine_RawSql
我尝试过这样使用它。
$q = new Doctrine_RawSql();
$q->select('{c.name}, {c.slug}, {sc.name}, {sc.slug}, {COUNT(bsc.id) AS business_count}');
$q->from('fi_category c LEFT JOIN fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL) LEFT JOIN (fi_business b INNER JOIN fi_business_subcategory bsc ON b.id = bsc.business_id AND (bsc.deleted_at IS NULL) INNER JOIN fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1) ON sc.id = bsc.subcategory_id');
$q->groupBy('GROUP BY c.id, sc.id');
$q->addComponent('c', 'Model_Category c');
$q->addComponent('sc', 'c.Subcategory sc');
$q->addComponent('bsc', 'sc.BusinessSubcategory bsc');
$q->addComponent('b', 'bsc.Business b');
$q->addComponent('su', 'b.Suburb su');
$q->execute();
此查询无效,这是为上述SQL查询构建Doctrine_Rawsql
的正确方法吗?
如果有人想查看这里的表格,则链接到小提琴http://sqlfiddle.com/#!2/5adaa
更新:这是此问题的延续,select query and count based on condition如果有人可以通过使用@MvG提供的解决方案中的任何查询帮助我构建Doctrine_Rawsql