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

时间:2014-11-06 10:44:04

标签: php html yii yii-chtml

我尝试通过属于该相册的图片显示指向相册/视图的链接。我有我的模特'专辑'在那里,我与我的照片' model,表示保存图片的表格(或更准确的图片名称,图片保存在文件夹中'上传':

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:我在 album / view 视图中显示一张专辑中的所有图片没有问题(但是这些交叉控制器,交叉表无论什么东西给我带来的困难)。在那里我嵌套了照片/索引视图,图片显示如下:

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

(作为链接,我显示图片的缩略图,为什么我有" / uploads / thumbs /"。" 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'),
            ...
        );
    }

我在专辑视图中访问User模型中的属性没有问题,检查了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>

1 个答案:

答案 0 :(得分:0)

要获得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)
);