gerhard
(gerhard@ecolar.co.za)
1
Hi guys
In the html view file that is used for CListView I have this:
<td>
<?php
if (!empty($data->belongsToRelation)){
echo ($data->belongsToRelation->fieldA); /* works fine */
}
if (!empty($data->hasManyRelation)){
echo ($data->hasManyRelation->fieldB); /* gives error */
}
?>
<td>
The belongsToRelation returns an OBJECT and works fine.
The hasManyRelation returns an ARRAY and gives error: "Trying to get property of non-object".
-
Is this only a problem in html (it is not a problem in CGridView)?
-
How do I display fieldB?
Thanx
Boontjie
(Boontjiesa)
2
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)
gerhard
(gerhard@ecolar.co.za)
3
Hi Boontjie
That stops the error but it still does not display fieldB. So it probably still needs something else.
Boontjie
(Boontjiesa)
4
Could you possible do a print_r and see how the data looks.
<pre>
print_r($data->hasManyRelation)
</pre>
alirz23
(Ali Raza)
5
returns an array. you might have to loop thru to get the values
gerhard
(gerhard@ecolar.co.za)
6
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.
e.g.
<td>
<?php foreach ($modelTabel1->relationToTabel2 as $Tabel2):?>
<?php echo $Tabel2->relationToTabel3->fieldInTabel3; ?>
<?php echo $Tabel2->relationToTabel3->relationToTable4->fieldInTabel4; ?>
<?php endforeach; ?>
</td>
Thanx for the help.