Noob question about relations and CGRIDVIEW

Hello everybody,

I start using Yii few days ago, I stuck on some problem which look very simple but I keep getting unexpected results.

I have two tables:

Constants table which have the following fields: (const_type, const_key), const_value

Case table which have the following details: (case_id), case_type, case_status, case_title,…

I want to show all records from case table that have case_status = 1 in a CGridView.

Instead of showing the case_type code I want to show his value which can be found in constants table (When const_type = 1 and const_key = case_type)

I want to represent in CActiveRecord relations the following query:

SELECT case_id,case_title,const_value

FROM const_table, case_table

WHERE const_table.const_key = case_table.case_type

AND case_table.case_status = 1

AND const_table.const_type = 1

In Case class I did the following:




	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(

			'constants'=>array(self::HAS_ONE, 'Constants', 'constant_type'),

		);

	}


	public function search()

	{

		$criteria=new CDbCriteria;

		$criteria->with='constants';

		$criteria->compare('case_status',1);

		$criteria->order='case_sent_timestamp ASC';

		return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria));

	}



In the view file I coded the following line:




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

	'id'=>'case-grid',

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

	'columns'=>array(

		'case_id',

            array(

                'name' => 'case_type',

                'value' => '$data->constants->constant_value',

            ),

		'case_status',

		'case_email',

		'case_subject',

		'case_description',

		'case_sent_timestamp',

		array(

			'class'=>'CButtonColumn',

		),

	),));



I didn’t receive the expected results.

May someone please guide me, I believe that I’m doing something completely wrong but I just can’t put my finger on it.

How can I transform the ID to it’s value with efficiency.

Thank you,

Bobo.

The issue is solved.

I change the relation declaration to:


'constants'=>array(self::HAS_MANY, 'Constants', '', 'on'=>'constant_key=case_type')