gpt4 book ai didi

php - yii中如何显示CGridview中相关表的数据

转载 作者:搜寻专家 更新时间:2023-10-31 20:41:49 24 4
gpt4 key购买 nike

我正在尝试使用 CGridView 显示结果。我有两个表 UsersproductsExiProducts是维护then之间多对多关系的表,关系名称为'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 '

),
));

有人可以告诉我如何获得在那里创建这些产品的用户的名称吗?users 表中的列是

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;


}

最佳答案

GridView View

$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')
)
),
));

这是你的 GridView value => array($this,'gridCreatedUser')这意味着 GridView 将在其 Controller 中搜索函数 gridCreateUser()

现在在 Controller 中

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 。

或者您可以通过在您正在创建其 GridView 的模型中定义模型之间的关系来以简单的方式使用

public function relations(){
return array(
'users' => array(self::HAS_MANY, 'Users', '<column_name>'),
);
}

然后你可以直接在你的 GridView 中访问它

$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
)
),
));

关于php - yii中如何显示CGridview中相关表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com