我有表格:categories HABTM sculptures hasMany images
从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'
)
)
)
)如果可能的话,我希望能够在阵列中获得与雕塑相关的图像?
发布于 2012-10-19 00:57:52
使用CakePHP的Containable Behavior。在阅读了相关内容并按照说明进行操作后,您的查找结果应该如下所示:
$this->Category->find('all', array(
'contain' => array(
'Sculpture' => array(
'Image'
)
)
));在Category模型中,您需要:
public $actsAs = array('Containable');
public $recursive = -1; //can set this in many places, but it must be -1 for the Containable find to work.发布于 2012-10-19 00:23:05
执行此操作的简单方法是在调用find() (example)时将recursive设置为2。这将告诉find()连接所有关联的模型以及与关联模型关联的模型。
但是,这种方法可能会导致数据集变得非常大,因此更好的方法是在包含更深层次的关联时使用containable behavior。
https://stackoverflow.com/questions/12957458
复制相似问题