我有一个包含许多照片的画廊模型,在我的画廊视图中,我找到了$slug的画廊。我正在显示属于特定gallery_id的所有照片。这和预期的一样工作。
但是,我想在查询中添加一个条件,仅当photo.active =>为'yes‘时才返回照片。
我发现因为图库视图是由$slug找到的,所以我过滤照片的尝试不起作用。在照片模型中,我保存的是gallery_id而不是gallery.slug。
我尝试在图库控制器中使用containable,但是在使用这个控制器find时,我得到了以下错误:
$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列是查询有条件照片的最佳方式吗?
另外,为什么在图库控制器中使用containable时会出现这个错误?
干杯,保罗
发布于 2014-02-03 23:37:38
您尝试将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);来获取数据。
https://stackoverflow.com/questions/21531298
复制相似问题