我有两张桌子,一张专辑和一张照片。照片表有一个FK(album_id),它引用了专辑表中的id。
现在我想在CListview中显示照片,但我不知道如何。 :(
我认为它使用的是dataProvider,我不知道如何将它与它结合起来。
你帮我吗? 谢谢大师。答案 0 :(得分:1)
这很简单
$dataProvider=new CActiveDataProvider('Photo', array(
'criteria'=>array(
'condition'=>'album_id=:album_id',
'params'=>['album_id'=>$album_id]
)
));
然后在视图中使用此dataProvider:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_photo'
));
答案 1 :(得分:0)
首先,您应该在模型中创建关系。 在Album.php中添加如下:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'photos'=>array(self::HAS_MANY, 'Photo',array('album_id'=>'id')),
);
}
类似于Photo.php:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'album'=>array(self::BELONGS_TO, 'Album',array('id'=>'album_id')),
);
}
现在您可以在代码中使用它:
$dataProvider=new CActiveDataProvider('Photo', array(
'criteria'=>array(
'condition'=>'album_id='.$yourAlbumId,
)
));
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
...
));
如果您需要参考Album
中的CListview
(假设您在相册模型中有一个名为“title”的字段),您可以将其称为$data->album->title
在您的商品模板中。