0

I have tables: categories HABTM sculptures hasMany images

from the CategoriesController#find() produces an array like so:

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'
                )
            )
    )
)

I'd like to be able to get the images related to sculpture in the array as well if this is possible?

4

2 回答 2

1

执行此操作的简单方法是在调用find()示例)时将递归设置为 2。这将告诉find()连接所有关联模型以及与关联模型关联的模型。

但是,这种方法可能会导致您的数据集变得非常大,因此更好的方法是在包含更深层次的关联时使用可包含的行为。

于 2012-10-18T16:23:05.293 回答
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.
于 2012-10-18T16:57:51.563 回答