Object Or Array Problem In Html View

Hi guys

In the html view file that is used for CListView I have this:



		if (!empty($data->belongsToRelation)){

			echo ($data->belongsToRelation->fieldA); /* works fine */


		if (!empty($data->hasManyRelation)){

			echo ($data->hasManyRelation->fieldB); /* gives error */




The belongsToRelation returns an OBJECT and works fine.

The hasManyRelation returns an ARRAY and gives error: "Trying to get property of non-object".

  1. Is this only a problem in html (it is not a problem in CGridView)?

  2. How do I display fieldB?


Hi I think what might be happening is that

Might be returning an array of records so for instance as a test:

echo ($data->hasManyRelation[0]->fieldB)

Hi Boontjie

That stops the error but it still does not display fieldB. So it probably still needs something else.

Could you possible do a print_r and see how the data looks.




returns an array. you might have to loop thru to get the values

Hi alirz23

Thanks for the reply.

I struggled a bit because I was tunneling through 4 tables with mixed one-to-many and many-to-one relations.

So basically, if you go from one-to-many, you have to use forEach() loop. But, if you go from many-to-one, you can just use model->relation->field.



	<?php foreach ($modelTabel1->relationToTabel2 as $Tabel2):?>

		<?php echo $Tabel2->relationToTabel3->fieldInTabel3; ?>

		<?php echo $Tabel2->relationToTabel3->relationToTable4->fieldInTabel4; ?>					

	<?php endforeach; ?>


Thanx for the help.