Populate Grid


I’m a new to yii.

I have 3 tables:












I want to display the Products in gridview and want to replace the SuppierID with CompanyName and the CategoryID with CategoryName.

I have searched this forum but could not find a clear explanation.

Any help would be appreciated.

define two relations for product model -

‘category’=>array(self::BELONGS_TO, ‘Category’, ‘category_id’),

‘supplier’=>array(self::BELONGS_TO, ‘Supplier’, ‘supplier_id’),

then simply use category.name and supplier.name in the gridview -

$this->widget(‘zii.widgets.grid.CGridView’, array(



    'name',          // display the product name attribute



check the api documentation for more details -



Thanx for the reply but I must be missing something.

Here is the relations I created in the Products.php model:

return array(

		'category'=>array(self::BELONGS_TO, 'Categories','CategoryID'),

		'supplier'=>array(self::BELONGS_TO, 'Sippliers','SupplierID'),


Here is the code to render it in the views/products/admin.php:

$this->widget(‘zii.widgets.grid.CGridView’, array(





























I get the following error:

htmlspecialchars() expects parameter 1 to be string, array given

I thought I knew OOP but this is just on a higher level. Please help.

Take a closer look at the second example here. Hint: alternate syntax.


Thanx guys, got it to work. If you can read you can fly!

hi guy,

based on CGridView reference, i always gt this error

PHP Error: Trying to get property of non-object , how to solve this problem


    $this->widget('zii.widgets.grid.CGridView', array(

        'id' => 'detail-grid',

        'dataProvider' => $dataProvider,

        //'filter' => $dataProvider,

        'columns' => array(


            'ope_comp_id.company_name', //this line can work 

            array(                  // error when want using this

                'name' => 'company_name',

                'value' => '$data->ope_comp_id->company_name',



                'class' => 'CButtonColumn', 

  $dataProvider = new CActiveDataProvider('user');

        $this->render('view', array(

            'dataProvider' => $dataProvider,