2015-12-03 40 views
-1

我是一个yiibie,我陷入了一个问题。我在我的查看文件中创建了一个名为ngopage.php的文件,我可以从我的Ngo表中获取数据,但我无法获得针对ngo针对ngo的特定ID编写任何注释的用户的Picture。用户的图片位于profile表中,评论来自userRateReviewNgos表。我也能够获得评论,但没有得到用户的照片。我尝试userRateReviewNgos模型的relation function中的through方法,该模型给出了一个例外Property "CBelongsToRelation.through" is not defined.请帮我解决这个问题。谢谢。 这是我的UserRateReviewNgos modelyii:无法使用“through”获取图片

<?php 

/** 
* This is the model class for table "user_rate_review_ngo". 
* 
* The followings are the available columns in table 'user_rate_review_ngo': 
* @property integer $id 
* @property integer $rate 
* @property string $review 
* @property integer $user_id 
* @property integer $ngo_id 
* 
* The followings are the available model relations: 
* @property Ngo $ngo 
* @property User $user 
*/ 
class UserRateReviewNgo extends CActiveRecord 
{ 
    /** 
    * Returns the static model of the specified AR class. 
    * @param string $className active record class name. 
    * @return UserRateReviewNgo the static model class 
    */ 
    public static function model($className=__CLASS__) 
    { 
     return parent::model($className); 
    } 

    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
     return 'user_rate_review_ngo'; 
    } 

    /** 
    * @return array validation rules for model attributes. 
    */ 
    public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      array('rate, review, user_id, ngo_id', 'required'), 
      array('rate, user_id, ngo_id', 'numerical', 'integerOnly'=>true), 
      array('review', 'length', 'max'=>500), 
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('id, rate, review, user_id, ngo_id', 'safe', 'on'=>'search'), 
     ); 
    } 

    /** 
    * @return array relational rules. 
    */ 
    public function relations() 
    { 
     // NOTE: you may need to adjust the relation name and the related 
     // class name for the relations automatically generated below. 
     return array(
      'ngo' => array(self::BELONGS_TO, 'Ngo', 'ngo_id'), 
      'user' => array(self::BELONGS_TO, 'User', 'user_id'), 
'profile' => array(self::BELONGS_TO, 'profile', 'id','through'=>'user'), 
        );// here i have user the through method for getting picture form profile table 
    } 

    /** 
    * @return array customized attribute labels (name=>label) 
    */ 
    public function attributeLabels() 
    { 
     return array(
      'id' => 'ID', 
      'rate' => 'Rate', 
      'review' => 'Review', 
      'user_id' => 'User', 
      'ngo_id' => 'Ngo', 
     ); 
    } 

    /** 
    * Retrieves a list of models based on the current search/filter conditions. 
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
    */ 
    public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id); 
     $criteria->compare('rate',$this->rate); 
     $criteria->compare('review',$this->review,true); 
     $criteria->compare('user_id',$this->user_id); 
     $criteria->compare('ngo_id',$this->ngo_id); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 
} 

此代码是从视图文件

<div class="profile"> 
              <div class="row" style="background-color:"> 
               <div class="col-md-4"> 
                <img src="<?php echo YII::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" class="img-responsive"><br> 
               </div> 
               <div class="col-md-4"> 
                <h2 class="profile-name" style="color:white;"><?php echo $model->ngo_name;?></h2> 
                <div class="rating"> 
                 <span class="glyphicon glyphicon-star 2x" aria-hidden="true"></span> 
                 <span class="glyphicon glyphicon-star 2x" aria-hidden="true"></span> 
                 <span class="glyphicon glyphicon-star 2x" aria-hidden="true"></span> 
                 <span class="glyphicon glyphicon-star 2x" aria-hidden="true"></span> 
                 <span class="glyphicon glyphicon-star 2x" aria-hidden="true"></span> 
                </div><!--rating ending here--> 
                <h3>Owner:</h3> 
                <p>Mr. Asad Abdul Jabbar</p> 
                 <h3>Address:</h3> 
                <address> 

               <p> <?php echo $model->address;?><br></p> 

              </address> 
                 <h3>Requirements:</h3> 
                 <p>-Volunteers required for refugees settlement in Kashmir<br>-Cash for food and water of refugees<br>-Donations can be transferred via online banking, AC no.<em>12345</em></p> 
                <h3>Write Review</h3> 
                <textarea type="text" class="form-control" rows="3" cols="4" name="requirements" value=""></textarea><br> 
                <button class="btn btn-primary">Share</button> 
               </div> 
               </div><!--row ending here--><br> 


              </div><!--profile ending here--><br> 

              <div class="story-content" style="background-color:white;"> 
              <div class="row"> 
               <div class="col-md-8 col-md-offset-2 vol-stories"> 
                <div class="media"> 
             <div class="media-left"> 
              <a href="user-profile.php"> 
               <?php $modelnew=$model->userRateReviewNgos; 
               foreach($modelnew as $new) 
               { 
               ?> 

               <img class="media-object" src="<?php echo YII::app()->request->baseUrl;?>/img/<?php echo $new->profile->picture;?>"> 
              </a> 
             </div> 
             <div class="media-body"><strong><?php echo $new->user->username; ?></strong> 
              <p style="color:black;"><?php echo $new->review;?></p> 
              <?php } 
               ?> 
               </div> 
                 <button class="btn btn-primary btn-xs">Edit</button> 
                 <button class="btn btn-primary btn-xs">Delete</button> 


               </div><!--Media ending here--> 

               </div> 
              </div><!--row ending here--><hr class="half"> 

              </div> 

回答

0

的ngopage.php为此,我认为你应该使用一个具有一个关系

'profile' => array(self::HAS_ONE, 'Profile', 'user_id'), 
+0

这可能对你的配置文件 – scaisEdge

+0

有帮助,我尝试了你的答案,在UserRateReviewNgo模型的关系函数中粘贴了一行,但没有获取图像。 –

+0

你有错误吗? – scaisEdge