我正在尝试使用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;
}
解决方法:
网格视图
$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')
)
),
));
这是您的网格视图值=> 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;
}
否,这会将所需的同名名称值发送到网格视图.
或者,您可以通过定义要创建其gridview的模型内部模型之间的关系来以简单的方式使用
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
)
),
));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。