2012-10-18 60 views
0

我有表:categories HABTM sculptures的hasMany images得到的HABTM關係的hasMany記錄

CategoriesController#find()產生一個數組,像這樣:

array(
    'Category' => array(
     'id' => '3', 
     'name' => 'Modern', 
    ), 
    'Sculpture' => array(
     (int) 0 => array(
      'id' => '25', 
      'name' => 'Ami', 
      'material' => 'Bronze', 
      'CategoriesSculpture' => array(
       'id' => '18', 
       'category_id' => '3', 
       'sculpture_id' => '25' 
      ) 
     ), 
     (int) 1 => array(
       'id' => '26', 
       'name' => 'Charis', 
       'material' => 'Bronze', 
       'CategoriesSculpture' => array(
        'id' => '19', 
        'category_id' => '3', 
        'sculpture_id' => '26' 
       ) 
      ) 
    ) 
) 

我希望能夠得到的圖像如果這是可能的話,與陣列中的雕塑有關嗎?

+0

看看'ContainableBehavior' – tigrang

回答

0

使用CakePHP的Containable Behavior。閱讀它,並按照指示後,您應該找到這個樣子:

$this->Category->find('all', array(
    'contain' => array(
     'Sculpture' => array(
      'Image' 
     ) 
    ) 
)); 

在您的分類模型,您需要:

public $actsAs = array('Containable'); 
public $recursive = -1; //can set this in many places, but it must be -1 for the Containable find to work. 
1

執行此操作的簡單方法是在調用find()example)時將遞歸設置爲2。這將告知find()連接所有關聯的模型以及與關聯模型關聯的模型。

但是,這種方法會導致您的數據集增長得相當大,因此更好的方法是在包含更深的關聯時使用containable behavior