2014-11-06 15 views
1

我試圖通過屬於該相冊的圖像顯示鏈接到專輯/查看。我有我的模型'相冊',在那裏我與我的'照片'模型有關係,它代表保存圖片的表格(或者更準確的圖片名稱,圖片保存在'上傳'文件夾中:Yii:CHtml圖像鏈接 - 如何通過其他表中的關係在一個表中獲取圖像?

public function relations() 
{ 
     return array(
        'photos' => array(self::HAS_MANY, 'Photo', 'album_id'), 
    ); 
} 

現在我顯示相冊/指數看法,在這裏我分析其中包含我所有的專輯$ 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), 

(因爲我顯示圖像的縮略圖的鏈接,這是爲什麼我有「/上傳/大拇指/」 「名」是表格,它表示的圖像的文件名)

編輯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($數據 - >所有者) :

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 '[email protected]' (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($數據 - > lastPhoto)(只是與它自己的屬性),但是當我嘗試用$數據 - > lastPhoto->命名它試圖獲取屬性的非對象,而$ 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> 
+0

'photos'=> array(self :: HAS_MANY,'Photo', 'album_id'),暗示你的模型有很多照片,所以在你看來,'$ data-> photos'可能是一組照片模型,而不是模型本身? – Stu 2014-11-06 10:46:26

+0

嗯,是有道理的(我是Yii的新手,所以我現在不太多)。 現在的問題是,如何獲得屬於該專輯的照片數組的第一個元素,並通過關係「照片」在我的視圖 – 2014-11-06 11:34:33

回答

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

中獲得屬性「名稱」我已經嘗試過,但我得到了錯誤: **未定義的偏移量:0 ** – 2014-11-06 11:45:47

+0

var_dump($ data-> photos);輸出是什麼? – Stu 2014-11-06 11:46:49

+0

來編輯: 嘗試過,並再次錯誤:**試圖獲得非對象** 的屬性,這裏是var_dump($ data-> lastPhoto)的輸出: http://pokit.org/get /?44503ed3e989f7a03185fda19dc88525.jpg – 2014-11-06 15:12:00

相關問題