2011-11-04 72 views
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 
       ) 

     ) 

) 

回答

1

對不起回答這麼快......我希望它可以幫助其他人有同樣的問題。這是解決方案。您基本上必須爲您的模型設置空的虛擬字段,然後用MyModel__VirtualFieldName對列進行別名。之後,它是完美的!

請注意,推特上的@jose_zap是CakePHP的核心開發者和超級,超級有用。再次感謝!

//Add the virtual fields for this specific case 
$this->User->Project->virtualFields += array(
    'TodayHours' => 0, 
    'WeekHours' => 0, 
    'SpentHours' => 0 
); 

//Run the query 
$projects = $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 Project__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 Project__WeekHours, 
     (SELECT SUM(hours) 
      FROM entries 
      WHERE project_id = Project.id 
       AND deleted = 0) as Project__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 
");