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;
}
Add relation in your users table by editing the relations() in your model page as shown
In your view.php ,
view of grid view
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
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
Then you can directly access it in you grid view
It is easy .
All you have to do is to write a relation in the model file ExiProducts
And you can use this in Grid view as