我正在尝试使用CGridView显示结果。我有两个表用户和产品。 ExiProducts 是保持当前多对多关系的表,让关系名称为'myrelation'
public function actionSearch() {
if(isset($_GET['searchButton'] && $_GET['searchType']==='products') {
$searchString= trim(strip_tags($_GET['searchValue']));
$model=new Products;
$criteria->compare('productName', $searchString, TRUE, 'AND', TRUE);
$criteria->compare('productType',$searchString,true,'OR',TRUE);
$criteria->compare('productBrand',$searchString,true,'OR',TRUE);
$criteria->compare('description',$searchString,true,'OR',true);
$dataProviderObj=new CActiveDataProvider($model, array(
'criteria'=>$criteria,
));
}
$this->render('search',array(
'dataProviderObj'=>$dataProviderObj,
'model'=>$model,
));
}
这是我的 view.php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProviderObj,
'columns'=>array(
'productName',
'productType',
'productBrand',
'description',
'I WANT THE NAME OF EVERY USER THAT CREATED THIS PRODUCT
HERE WHICH IS IN THE USERS TABLE '
),
));
有人可以告诉我如何获得在那里创建这些产品的用户的名字。 用户表中的列
UserId,
Username
和 ExiProducts 是
UserId,
ProductId
更新了我的代码
public function gridCreateUser($data,$row) {
$myproducts=array();
$user = $data->userId;
$records= Users::model()->with('usersproducts')->findAll('userId=:userId',array(':userId'=>$user));
foreach($records as $record)
{
foreach($record->usersproducts as $productis)
{
$myproducts[]=$productis->productName;
}
}
return $myproducts;
}
答案 0 :(得分:1)
网格视图视图
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProviderObj,
'columns'=>array(
'productName',
'productType',
'productBrand',
'description',
array(
'name' => '<column_name>'
'value' => array($this,'gridCreateduser')
)
),
));
这是网格视图value => array($this,'gridCreatedUser')
这意味着网格视图将在其控制器中搜索函数 gridCreateUser()
现在在控制器中
public function gridCreateUser($data,$row){
$user = $data-><colmn_name>;
//do your stuff for finding the username or name with $user
//for eg.
$detail = User::model()->findByPk($user);
// make sure what ever model you are calling is accessible from this controller other wise you have to import the model on top of the controller above class of the controller.
return $detail->username;
}
否则会将该库仑名称的所需值发送到网格视图。
或者您可以通过定义您正在创建网格视图的模型内的模型之间的关系来以简单的方式使用
public function relations(){
return array(
'users' => array(self::HAS_MANY, 'Users', '<column_name>'),
);
}
然后您可以在网格视图中直接访问它
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProviderObj,
'columns'=>array(
'productName',
'productType',
'productBrand',
'description',
array(
'name' => '<column_name>'
'value' => $data->users->username
)
),
));
答案 1 :(得分:1)
很容易。
您所要做的就是在模型文件ExiProducts中编写关系
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(
'user' => array(self::BELONGS_TO, 'users', 'UserId'),
);
}
您可以在网格视图中将其用作
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProviderObj,
'columns'=>array(
'productName',
'productType',
'productBrand',
array(
'header'=>'User Name',
'value'=>'CHtml::encode($data->user->Username)',//This will use the relationship and get all the details of the paticular user from users table
),
),
));
答案 2 :(得分:1)
通过编辑模型页面中的relations()来在users表中添加关系,如图所示
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(
'user' => array(self::BELONGS_TO, 'users', 'UserId'),
);
}
在你的view.php中,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProviderObj,
'columns'=>array(
'productName',
'productType',
'productBrand',
'description',
array(
'name'=>'User Name',
'value'=>$model->->user->Username,
),
),
));