Displaying value from related table

I have 2 models - AdRates and AdTypes. The relationship between them is Adrates has many Adtypes and Adtypes has one AdRates. In my relations definition in AdRates model, I have defined the relationship as:




public function relations()

	{

		

	return array(

          'adtypes'=>array(self::HAS_MANY, 'AdTypes', 'adtype_id')       

        );

	}



What I want to do is in the view file for AdRates, instead of displaying just the foreign key adtype_id, I want to display the description. So I defined the attributes in the view file as the following:





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

    'data'=>$data,

    'attributes'=>array(

        'num_issues',          

        'rate',

		array(

                        'name' => 'adtype_id',

                        'value' => '$data->adtypes->description',

                		

    ),

)));



Below that I have the command to display the value:




<?php echo CHtml::encode($data->adtype_id); ?>



The problem I am having is instead of displaying the actual value, what shows is literally ‘$data->adtypes-description’ rather than something like “Large Ad”. What am I doing wrong?




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

    'data'=>$data,

    'attributes'=>array(

        'num_issues',          

        'rate',

                array(

                        'name' => 'adtype_id',

                        'value' => $data->adtypes->description,

                                

    ),

)));



:)

or even easier:




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

    'data'=>$data,

    'attributes'=>array(

        'num_issues',          

        'rate',

	'adtypes.description',

    ),

));



Thank you so much for your suggestions. I tested each one and here are the results:

Testing using without quotations at all, i.e.

‘value’ => $data->adtypes->description,

Result:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘adtypes.adtype_id’ in ‘where clause’. The SQL statement executed was: SELECT adtypes.id AS t1_c0, adtypes.description AS t1_c1 FROM tbl_adtypes adtypes WHERE (adtypes.adtype_id=:ypl0)

Second test using dot notation, i.e.

‘adtypes.description’,

This did not create a fatal error but the resulting display was:

Ad Rates

Num Issues 2

Rate 50

Adtype adtypes.description

Adtype:

So back to the beginning. What could be the problem? Did I define the relations incorrectly? Somehow the linkage between two tables does not seem to be recognized by Yii but I can’t figure out what I did wrong.

AdRates

id

rate

adtype_id

AdTypes

id

description

There really isn’t much here. It is just a simple one to many relationship.

Hi,

according to your model, you can have several adtypes for an adrate. If this is what you really want, you can’t show only one description, because there are potentially many.

For that case, i have complete my view form with CGridView of the related model (i use the multimodelform extension).

Hope it will help.

You must also declare the relation for the other table.

does AdTypes table has adtype_id column? when you define HAS_MANY relation the referred model should have such column that points to AdRates primary key. if the relation is in different way - use BELONGS_TO relation type and then specified adtype_id columnt i AdRates should point to AdTypes primary key.

Ah, sorry. I’ve made a mistake. ragua is right.

$data->adtypes should be an array because it’s a HAS_MANY relation.

So, the code should be something like this:




$descs = array();

foreach($data->adtypes as $adtype)

{

    $descs[] = $adtype->description;

}

$description = implode(',', $descs);


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

    'data'=>$data,

    'attributes'=>array(

        'num_issues',          

        'rate',

	array(

            'name' => 'adtype_id',

            'value' => $description,

        ),

    )

));



Thanks. I found out that using cgridView did the trick as follows. Notice that $data->adtypes->description is enclosed in single quotes:

//view file




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

	'id'=>'ad-rates-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

            'name'=>'adtype_id',

            'value'=>'$data->adtypes->description',

        ),

		'num_issues',

		'rate',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>