Search code examples
phpyiicgridview

How to display data from related tables in CGridview in yii


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;


}

Solution

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