Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii:CHtml图像链接-如何通过另一个表中的关系获取一个表中的图像?_Php_Html_Yii_Yii Chtml - Fatal编程技术网

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

Php Yii:CHtml图像链接-如何通过另一个表中的关系获取一个表中的图像?,php,html,yii,yii-chtml,Php,Html,Yii,Yii Chtml,我试图通过属于该相册的图像显示指向该相册/视图的链接。我有我的“相册”模型,在那里我有与我的“照片”模型的关系,它表示保存图片的表格。或者更准确地说,图片保存在“上传”文件夹中: public function relations() { return array( 'photos' => array(self::HAS_MANY, 'Photo', 'album_id'), ); } 现在我正在显示相册/索引视图,在这里我

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

public function relations()
{
        return array(
                    'photos' => array(self::HAS_MANY, 'Photo', 'album_id'),
    );
}
现在我正在显示相册/索引视图,在这里我解析$dataProvider对象,它包含我的所有相册。现在$dataProvider被解析为一个_视图,在那里我试图访问图片,实际上我只需要每个相册中显示的属于每个相册的一张图片。该图片将表示指向相册/视图的链接,从中可以看到属于该相册的所有图片。我不知道怎么做。我试过:

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

@斯图

在我的相册中,我有三张照片:

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);
    }
}
视图文件为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>

要获取照片关系中的第一项,您可以调用第一个数组值,尽管您可能需要添加一些额外的条件,以防它是空数组而根本没有值

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

photos'=>arrayself::HAS_MANY,'Photo','album_id',意味着你的模型有很多照片,所以在你看来,$data->photos可能是一组照片模型,而不是模型本身?是的,这很有道理,我是Yii的新手,所以我现在不太了解。现在的问题是,如何获取属于该相册的照片数组的第一个元素,并通过关系“photos”在我的视图中获取属性“name”,我已经尝试过了,但我得到了错误:未定义的偏移量:0 var_转储$data->photos的是什么;输出?编辑:重试,错误:尝试获取非对象的属性,这是var_dump$data->lastPhoto的输出:这很奇怪,从var dump中可以看到,Photo对象在_attributes数组中有一个带有值的名称。这应该通过$data->lastPhoto->name输出。您能否将您在此处调用的控制器操作、相册模型和正在渲染的视图文件发布为问题的编辑?一定有别的东西阻止你访问它,好像var_dump显示那里有一个值,我看不出它在你调用它时不输出的原因。我明白了!我正在读你的答案,我突然想到。。。我试图获得非对象错误的属性,因为一个测试相册中没有图片。我已经删除了那张专辑,现在一切正常。这是一个愚蠢的错误。我没有输入任何代码来验证变量是否为空。现在在我看来,我有
CHtml::link(
    CHtml::image(Yii::app()->request->baseUrl."/uploads/thumbs/".$data->lastPhoto->name),
    array('view', 'id'=>$data->id)
);