0

我有一个包含许多照片的画廊模型,在我的画廊视图中,我通过 $slug 找到了画廊。我正在显示属于特定 gallery_id 的所有照片。这按预期工作。

但是,我想在查询中添加一个条件,仅在 photo.active => 'yes' 时返回照片。

我发现,因为画廊视图是由 $slug 找到的,所以我过滤照片的尝试不起作用。在照片模型中,我保存了 gallery_id 而不是 gallery.slug。

我尝试在画廊控制器中使用可包含但在使用此控制器查找时出现以下错误:

    $galleryphotos = $this->Gallery->Photo->find('all', array(
    'contain' => array('Photo'),
    'conditions' => array(
        'gallery.slug'=> $slug,
        'photo.active'=>'yes')));   

我在视图中看到的错误:

Warning (512): Model "Photo" is not associated with model "Photo"

在照片模型中添加gallery.slug 列是查询有条件的照片的最佳方式吗?

另外,为什么在画廊控制器中使用可包含时会出现错误?

干杯,保罗

4

1 回答 1

0

您尝试将 Photo 模型添加到您正在查询的 Photo 模型中,因此请尝试:

$galleryphotos = $this->Gallery->find('first', array(
    'contain' => array('Photo'),
    'conditions' => array(
        'Gallery.slug'=> $slug,
        'Photo.active'=>'yes')));  

或者

$galleryphotos = $this->Gallery->Photo->find('all', array(
    'contain' => array('Gallery'),
    'conditions' => array(
        'Gallery.slug'=> $slug,
        'Photo.active'=>'yes')));

假设关联是Gallery hasMany Photo

这应该在模型内部,而不是控制器中,这样您就可以保持控制器的精简并简单地调用$this->Gallery->view($slug);以获取数据。

于 2014-02-03T15:37:37.527 回答