Yii:CHtml图像链接-如何通过其他表中的关系获取一个表中的图像



我正试图通过属于相册的图像来显示指向该相册的链接/view。我有我的模型"相册",在那里我有我与"照片"模型的关系,它代表保存图片的表格(或者更准确地说,图片名称保存在文件夹"上传"中:

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

我得到了错误:试图获取非对象的属性

附言:我在相册/视图视图中显示一个相册中的所有图片没有问题(但这些交叉控制器,交叉表,无论什么东西都让我很难)。在那里我嵌套了照片/索引视图,图片显示如下:

echo CHtml::link(
                    CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->name),
                    Yii::app()->request->baseUrl."/uploads/".$data->name ),

(作为一个链接,我正在显示图像的缩略图,这就是为什么我有"/uploads/tumbs/"。"name"是表中代表图像文件名的列)

编辑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时,它是尝试获取非对象的属性,而$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,
        ));
    }

视图文件是album/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>

要获得photos关系中的第一个项,您可以只调用第一个数组值,尽管您可能需要设置一些额外的条件,以防它是一个空数组并且根本没有值。

CHtml::link(
    CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->photos[0]->name),
    array('view', 'id'=>$data->id)
);

编辑

另一种选择是将关系限制为只拉一行,而不是拉所有关系并只使用第一行:

public function relations()
{
    return array(
        ...
        'lastPhoto' => array(self::HAS_ONE, 'Photo', 'album_id', 'order'=>'lastPhoto.id DESC'),
        ...
    );
}

这样打电话:

CHtml::link(
    CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->lastPhoto->name),
    array('view', 'id'=>$data->id)
);

最新更新