Relations in the "view" view / CHtml::tag()


I’m trying to use a relation in the “view” view created by the gii crud. Seems to be a pretty common thing right? I’m currently using ternaries for my zero to many relationships so I can avoid the “Trying to get property of non-object” error. This is my current code:







but that gives me a blank spot instead of the default “Not Set” that yii puts out if it can’t find an ID.

I looked at my page source and noticed that Yii outputs a

<span class='null'>Not Set</span>

element so I modified my code above to have

'value'=>$model->taskID?$model->task->task:CHtml::tag('span', array('class'=>'null'), 'Not Set', true)

but this literally outputs all of that in the field because it generates &lt; and &gt; rather than the <> for the tags.

Question #1: How do I get the CHtml::tag() function to output the actual brackets for the tags or is there another function that I can put it in to achieve this?

Question #2: This seems like something very common. If I want to achieve the same result as the yii defaults while using relations, is this how I should be going about it or is there something built in that I’m missing?



Ok, I can answer my own question. Well at least the second one. I didn’t realize there was a relation syntax for the CDetailView. The tutorials that I’ve used never used it. CDetailView Documentation

And I think I answered my first question as well. If I set the type to ‘raw’ it seems to output the tags properly.

You can also use CHtml:value($model, ‘relation.attribute’) to display attribute related attributes and not worry about the property not set error. this is what cdetailview uses internally