0
我的項目中有一個複雜的查詢,我在本機SQL中運行。在查詢中命名MySQL集合函數的CakePHP 2.0
$r = $this->User->Project->query("
SELECT
Project.id,
Project.name,
Customer.name,
(SELECT SUM(hours)
FROM entries
WHERE project_id = Project.id
AND createdby = $id
AND deleted = 0
AND YEAR(entry_date) = YEAR(NOW())
AND DAYOFYEAR(entry_date) = DAYOFYEAR(NOW())) as TodayHours,
(SELECT SUM(hours)
FROM entries
WHERE project_id = Project.id
AND createdby = $id
AND deleted = 0
AND YEAR(entry_date) = YEAR(NOW())
AND WEEKOFYEAR(entry_date) = WEEKOFYEAR(NOW())) as WeekHours,
(SELECT SUM(hours)
FROM entries
WHERE project_id = Project.id
AND deleted = 0) as SpentHours,
Project.budget_hours as TotalHours
FROM projects as Project
INNER JOIN users_projects UserProject on UserProject.project_id = Project.id
AND UserProject.user_id = $id
INNER JOIN customers Customer on Customer.id = Project.customer_id
WHERE Project.deleted = 0
AND Project.archived = 0
");
當結果復甦時,幾乎一切都看起來很完美。唯一的問題是CakePHP將我的SUM
列拋入他們自己的分組[0]
。如果可能的話,我想讓他們加上Project
。有什麼辦法可以混淆這些字段,這樣會發生嗎?
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 7
[name] => Some Project Name
[TotalHours] => 67.00
)
[Customer] => Array
(
[name] => Some Customer Name
)
[0] => Array
(
[TodayHours] => 2.25
[WeekHours] => 27.25
[SpentHours] => 27.25
)
)
)