问题
I am trying to display the results using CGridView. i have two tables Users and products. ExiProducts is the table which maintains the many to many relation between then and let the relation name is '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,
));
}
This is my 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 '
),
));
Can somebody please tell me how i can get the name of the users creating those products there. columns in users table are
UserId,
Username
and ExiProducts are
UserId,
ProductId
Updated my code
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;
}
回答1:
view of grid 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')
)
),
));
This is you grid view value => array($this,'gridCreatedUser')
this means that grid view will search a function in its controller for a function gridCreateUser()
Now in 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;
}
No this will send the desired value of that coulmn name to grid view.
Or you can use in a simple manner by defining relation between models inside model whose gridview you are creating
public function relations(){
return array(
'users' => array(self::HAS_MANY, 'Users', '<column_name>'),
);
}
Then you can directly access it in you grid view
$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
)
),
));
回答2:
It is easy .
All you have to do is to write a relation in the model file 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'),
);
}
And you can use this in Grid view as
$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
),
),
));
回答3:
Add relation in your users table by editing the relations() in your model page as shown
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'),
);
}
In your 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,
),
),
));
来源:https://stackoverflow.com/questions/20285509/how-to-display-data-from-related-tables-in-cgridview-in-yii