我有这个 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();
此查询不起作用,这是Doctrine_Rawsql
为上述 SQL 查询构建的正确方法吗?
如果有人想检查这里的表格是小提琴http://sqlfiddle.com/#!2/5adaa的链接
更新:这是这个问题的延续, 如果有人可以通过使用@MvG 提供的解决方案中的任何查询来帮助我构建,请根据条件选择查询和计数Doctrine_Rawsql