2012-08-02 73 views
0

我有模型命名爲地方類別它們使用HABTM關係連接。現在,當我從地方讀取特定字段時,我在結果中獲得了其他字段。Cakephp HABTM刪除額外的域

array(
'Place' => array(
    'id' => '8' 
), 
'Category' => array(
    (int) 0 => array(
     'id' => '2', 
     'CategoriesPlace' => array(
      'id' => '673', 
      'place_id' => '8', 
      'category_id' => '2' 
     ) 
    ), 
    (int) 1 => array(
     'id' => '3', 
     'CategoriesPlace' => array(
      'id' => '674', 
      'place_id' => '8', 
      'category_id' => '3' 
     ) 
    ), 
    (int) 2 => array(
     'id' => '5', 
     'CategoriesPlace' => array(
      'id' => '675', 
      'place_id' => '8', 
      'category_id' => '5' 
     ) 
    ) 
) 

我不想顯示該CategoriesPlace陣列。任何想法如何?

回答

1

兩個CakePHP的解決方案:

$this->Place->recursive=-1; 

查找之前。

或者:

$this->Place->contain(); 

contain docs

編輯: 對於輸出的更大控制:

containable docs

地說:

public $actsAs = array('Containable'); 

在模型中。

然後:

$this->Place->find('all', array(
    'contain' => array(
     'Place', 
     'Category' => array(
      'fields'=>array('id') 
     ) 
    ) 
) 
); 

它可能無法正常工作的CakePHP添加所有需要執行查詢的外鍵。

+0

'陣列( \t(INT)0 =>數組( \t \t '地點'=>數組( \t \t \t 'ID'=> '1' \t \t) \t) )' 現在這是我得到的結果。但是我需要** Category **數組在那裏,只是不需要** CategoriesPlace **數組 – dinkan 2012-08-02 16:06:38

+0

啊。然後你使用可容納的。我會更新答案。 – 2012-08-02 16:17:22

+0

它仍然不過濾我認爲的HABTM關係領域。 http://stackoverflow.com/questions/4300262/containable-wont-filter-2nd-level-model-with-habtm-hasmany – dinkan 2012-08-02 16:46:52