2013-02-12 47 views
0

my use case從嵌套列表中選擇一個字段

我有一個用例如上。在我CategoriesController.php,我做我的Category查詢象下面這樣:

$category = $this->Category->Transaction->find("first", array(
    "fields" => array("Category.*", "COUNT(*) AS TOTAL", "AVG(Transaction.debit) AS AVERAGE_COST"), 
    "conditions" => array("not" => array("Transaction.debit" => null)), 
    "contain" => array("Category" => array("CategoryType")), 
    "group" => "Transaction.category_id", 
    "order" => array("TOTAL" => "desc"), 
)); 

,這就是我得到的回報:

Array 
(
    [Category] => Array 
     (
      [id] => 2 
      [name] => FOOD 
      [category_type_id] => 2 
     ) 

    [0] => Array 
     (
      [TOTAL] => 4596 
      [AVERAGE_COST] => 7.668451 
     ) 

) 

問題:我怎麼可以指定在返回領域CategoryType

我用Category.CategoryType.*CategoryType.*嘗試,並沒有工作。但是,如果我完全取出fields數組並讓cakephp默認處理,cakephp設法返回數組中的CategoryType。

回答

0

一對夫婦的事情,尋找在這裏:

  1. 你連接中可容納的行爲你category_type模式?從書中獲得:當使用'fields'和'contain'選項時 - 要小心包括所有您的查詢直接或間接需要的外鍵。另請注意,由於Containable必須附加到所有用於收容的模型,您可以考慮將其附加到您的AppModel。

  2. 由於您在可容納參數之外分配fields,這可能會覆蓋您的可容納行爲。考慮將"fields" => array("Category.*", "COUNT(*) AS TOTAL", "AVG(Transaction.debit) AS AVERAGE_COST"),移動到您的包含數組中。