我正在尝试通过属于该相册的图像显示指向相册/视图的链接。我有我的模型“相册”,并且与我的“照片”模型有关系,它代表保存图片的表格(或者更准确的图片名称,图片保存在“上传”文件夹中:
public function relations()
{
return array(
'photos' => array(self::HAS_MANY, 'Photo', 'album_id'),
);
}
现在我正在显示专辑/索引视图,我在其中解析包含我所有专辑的 $dataProvider 对象。现在 $dataProvider 被解析为_view视图,我正在尝试访问属于显示的每个相册的图片(实际上我只需要每个相册一张图片)。该图片将代表一个相册/视图视图的链接,在该视图中人们可以看到属于该相册的所有图片。我不知道该怎么做。我试过:
CHtml::link(
CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->photos->name),
array('view', 'id'=>$data->id));
我得到了错误:尝试获取非对象的属性
PS:我在相册/视图视图中显示一个相册中的所有图片没有问题(但是这些交叉控制器,交叉表无论什么事情都让我很难过)。我有嵌套的照片/索引视图,图片显示如下:
echo CHtml::link(
CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->name),
Yii::app()->request->baseUrl."/uploads/".$data->name ),
(作为链接,我正在显示图像的缩略图,这就是为什么我有“/uploads/thumbs/”。“名称”是表中的列,代表图像的文件名)
编辑 1
@Stu
在我的相册中,我有三张照片:
array (size=3)
0 =>
object(Photo)[84]
private '_uploads' => null
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=6)
'id' => string '1' (length=1)
'album_id' => string '1' (length=1)
'name' => string 'WP_20140907_18_34_12_Pro.jpg' (length=28)
'caption' => string 'Balerina' (length=8)
'date_created' => string '2014-10-27 19:56:13' (length=19)
'date_updated' => null
private '_related' (CActiveRecord) =>
array (size=0)
empty
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '1' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
1 =>
object(Photo)[85]
private '_uploads' => null
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=6)
'id' => string '2' (length=1)
'album_id' => string '1' (length=1)
'name' => string 'WP_20140907_18_49_33_Pro.jpg' (length=28)
'caption' => string '' (length=0)
'date_created' => string '2014-10-27 19:56:45' (length=19)
'date_updated' => null
private '_related' (CActiveRecord) =>
array (size=0)
empty
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '2' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
2 =>
object(Photo)[86]
private '_uploads' => null
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=6)
'id' => string '3' (length=1)
'album_id' => string '1' (length=1)
'name' => string 'WP_20140907_18_49_38_Pro.jpg' (length=28)
'caption' => string '' (length=0)
'date_created' => string '2014-10-27 20:00:41' (length=19)
'date_updated' => null
private '_related' (CActiveRecord) =>
array (size=0)
empty
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '3' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
编辑 2
这真的让我发疯......在同一个专辑模型中,我与其他表“用户”有其他关系:
public function relations()
{
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
...
);
}
而且我在专辑视图中访问用户模型中的属性没有问题,检查了 var_dump($data->owner):
object(User)[83]
public 'passSave' => null
public 'passRepeat' => null
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=7)
'id' => string '1' (length=1)
'email' => string 'fdhghhg@gmail.com' (length=25)
'username' => string 'admin' (length=5)
'firstname' => string 'Emina' (length=5)
'lastname' => string 'Hasanović' (length=10)
'pass' => string 'sa1aY64JOY94w' (length=13)
'date_created' => string '2014-10-27 11:20:55' (length=19)
private '_related' (CActiveRecord) =>
array (size=0)
empty
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '1' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
它与 var_dump($data->lastPhoto) 的输出完全相同(只是具有它自己的属性),但是当我尝试使用 $data->lastPhoto->name 时,它正在尝试获取 non-object 的属性,而 $data- >owner->username 效果很好
编辑 3
这是我的专辑模型:
<?php
/**
* This is the model class for table "album".
*
* The followings are the available columns in table 'album':
* @property string $id
* @property string $name
* @property string $owner_id
* @property integer $shareable
* @property string $date_created
*
* The followings are the available model relations:
* @property User $owner
* @property Photo[] $photos
*/
class Album extends CActiveRecord
{
public function tableName()
{
return 'album';
}
public function rules()
{
return array(
array('name', 'required'),
array('shareable', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>100),
array('owner_id', 'length', 'max'=>10),
array('name', 'safe', 'on'=>'search'),
);
}
protected Function beforeSave() {
if(parent::beforeSave()) {
if($this->isNewRecord) {
$this->owner_id=Yii::app()->user->getId();
}
return true;
}
else
return false;
}
public function relations()
{
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'photos' => array(self::HAS_MANY, 'Photo', 'album_id'),
'lastPhoto' => array(self::HAS_ONE, 'Photo', 'album_id', 'order'=>'lastPhoto.id DESC'),
);
}
public function scopes()
{
return array(
'mine'=>array(
'order'=>'date_created DESC',
'condition'=>'owner_id=:owner',
'params'=>array(
'owner'=>Yii::app()->user->getId(),
)
)
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'owner_id' => 'Owner',
'shareable' => 'Shareable',
'date_created' => 'Date Created',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('shareable',$this->shareable);
$criteria->scopes='mine';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
控制器动作是(来自 AlbumController):
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Album', array(
'criteria'=>array(
'condition'=>'shareable=1',
'order'=>'date_created DESC'
)
)
);
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
视图文件是专辑/index.php:
<h1>Albums</h1>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
_view.php:
<div class="view">
<h><?php // echo CHtml::encode($data->name); ?></h1>
<h1>
echo CHtml::link(
CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs".$data->lastPhoto->name),
array('view', 'id'=>$data->id));
?>
</h1>
<br />
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('shareable')); ?>:</b>
<?php echo CHtml::encode($data->shareable)? 'Public' : 'No'; ?>
<br />
<b><?php echo 'Created by'; ?>:</b>
<?php echo CHtml::encode($data->owner->fullName()); ?>
<b><?php echo 'on Date'; ?>:</b>
<?php echo CHtml::encode($data->date_created); ?>
<br />
</div>