yii:无法使用“通过"获取图片;
我是一个 yiibie,我遇到了一个问题.我在我的视图文件中创建了一个名为 ngopage.php
的文件,我能够从我的 Ngo
表中获取数据,但我无法获取 Picture
正在针对 ngo
的特定 id 为 ngo
编写任何评论的用户.用户的照片在 profile
表中,评论来自 userRateReviewNgos
表.我也能够获得评论,但无法获得用户的照片.我在 userRateReviewNgos
模型的 relation function
中尝试了 through
方法,它给出了一个异常 Property "CBelongsToRelation.through" is not defined.
请帮我解决这个问题.谢谢.这是我的 UserRateReviewNgos 模型
500),//以下规则由 search() 使用.//请删除那些不应搜索的属性.array('id, rate, review, user_id, ngo_id', 'safe', 'on'='search'),);}/*** @return 数组关系规则.*/公共函数关系(){//注意:您可能需要调整关系名称和相关的//下面自动生成的关系的类名.返回数组('非政府组织' =>数组(自我:: BELONGS_TO,'Ngo','ngo_id'),'用户' =>数组(自我:: BELONGS_TO,'用户','user_id'),'个人资料' =>数组(self::BELONGS_TO,'profile','id','through'=>'user'),);//这里我有用户获取图片表单配置文件表的through方法}/*** @return 数组自定义属性标签(name=>label)*/公共函数attributeLabels(){返回数组('id' =>'ID','率' =>'速度','评论' =>'审查','user_id' =>'用户','ngo_id' =>'恩',);}/*** 根据当前搜索/过滤条件检索模型列表.* @return CActiveDataProvider 可以根据搜索/过滤条件返回模型的数据提供者.*/公共功能搜索(){//警告:请修改以下代码以删除属性//不应该被搜索.$criteria=新的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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,));}}
这是视图文件中的ngopage.php
<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">
<div class="col-md-4"><h2 class="profile-name" style="color:white;"><?php echo $model->ngo_name;?></h2><div class="评级"><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><!--评分在此结束--><h3>所有者:</h3><p>先生阿萨德·阿卜杜勒·贾巴尔</p><h3>地址:</h3><地址><p><?php echo $model->address;?><br></p></地址><h3>要求:</h3><p>-难民在克什米尔定居所需的志愿者<br>-难民的食物和水的现金<br>-捐款可以通过网上银行转账,AC no.<em>12345</em>/><h3>撰写评论</h3><textarea type="text" class="form-control" rows="3" cols="4" name="requirements" value=""></textarea><br><button class="btn btn-primary">分享</button>
</div><!--行在此结束--><br></div><!--配置文件在此结束--><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 作为 $new){?><img class="media-object" src="<?php echo YII::app()->request->baseUrl;?>/img/<?php echo $new->profile->图片;?>"></a>
<div class="media-body"><strong><?php echo $new->user->username;?></strong><p style="color:black;"><?php echo $new->review;?></p><?php }?>
<button class="btn btn-primary btn-xs">编辑</button><button class="btn btn-primary btn-xs">删除</button></div><!--媒体在此结束-->
</div><!--行在此结束--><hr class="half">
解决方案 为此,我认为您应该使用具有一个关系
'profile' =>数组(自我:: HAS_ONE,'配置文件','user_id'),
I am a yiibie, and i am stuck in a problem. I have made a file named ngopage.php
in my view file and i am able to get the data from my Ngo
table but I am unable to get the Picture
of the user who is writing any comment for an ngo
against a specific id of an ngo
. The picture of the user is in the profile
table and the reviews are coming from the userRateReviewNgos
table. I am also able to get the reviews but not getting the picture of the user. I tried the through
method in the relation function
of the userRateReviewNgos
model which gives an exception Property "CBelongsToRelation.through" is not defined.
Please help me with this. thank you.
This is the code for my UserRateReviewNgos model
<?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,
));
}
}
This is the ngopage.php from view file
<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>
解决方案
For this i think you should use a has one relation
'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
相关文章