Populate Grid

Hi,

I’m a new to yii.

I have 3 tables:

Products

-ProductID

-ProductName

-SupplierID

-CategoryID

Suppliers

-SupplierID

-CompanyName

Categories

-CategoryID

-CategoryName

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(

'dataProvider'=>$dataProvider,


'columns'=>array(


    'name',          // display the product name attribute


    'category.name', 


    'supplier.name'

check the api documentation for more details -

http://www.yiiframework.com/doc/api/1.1/CGridView

Hi,

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(

'id'=>'products-grid',


'dataProvider'=>$model->search(),


'filter'=>$model,


'columns'=>array(


	//'ProductID',


	'ProductName',


	array(


		'name'=>'SupplierID',


		'value'=>'supplier.CompanyName',


	)


	,


	array(


		'name'=>'Category',


		'value'=>'category.CategoryName',


	),


	'QuantityPerUnit',


	'UnitPrice',


	/*


	'UnitsInStock',


	'UnitsOnOrder',


	'ReorderLevel',


	'Discontinued',


	*/


	array(


		'class'=>'CButtonColumn',


	),


),

));

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.

/Tommy

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


 <?php

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

        'id' => 'detail-grid',

        'dataProvider' => $dataProvider,

        //'filter' => $dataProvider,

        'columns' => array(

            'id',

            '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',

            ),


            array(

                'class' => 'CButtonColumn', 




  $dataProvider = new CActiveDataProvider('user');

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

            'dataProvider' => $dataProvider,

        ));