Newbie Question on CDetailView

I am fairly new to Yii and trying to grasp how to display related array of data within a CDetailView. See last field…


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

	'data'=>$model,

	'attributes'=>array(

		array(

			'name'=>'customer_id',

		    'value'=>CHtml::encode($model->customer->name)

		),

        array(

			'name'=>'owner_id',

		    'value'=>CHtml::encode($model->owner->name)

		),


        array(

			'name'=>'billed_service',

		    'value'=>CHtml::encode($model->customer->getBillTypeText())

		),

        'units_billed',

		'dos',

		'reason_for_cancel',


         array(

			'name'=>'occupation_type',

		    'value'=>'How to Display an Array of Data?'

		),


	),

)); ?>

In my last field the ‘occupation_type’ can have up to three values of data. Is this possible within the CDetailView?

Thanks in advance for those willing to help!

Erik

Hi,

If you want them to be displayed in one row, then you can do something like this …




<?php 

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

	'data'=>$model,

	'attributes'=>array(

		array(

			'name'=>'customer_id',

			'value'=>$model->customer->name

		),

		...

		array(

			'header'=>'Occupations',

			'type'=>'ntext',

			'value'=>implode("\n", $occupation_types),

		),

	),

)); 

?>



Or if you want them to be displayed in separated rows, you can construct the array for the attributes outside of the function call.




<?php

$attributes = array(

	array(

		'name'=>'customer_id',

		'value'=>$model->customer->name

	),

	...

);

foreach($occupation_types as $occupation)

{

	$attributes[] = array(

		'header'=>'Occupation',

		'value'=>$occupation,

	);

}

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

	'data'=>$model,

	'attributes'=>$attributes,

)); 

?>



Note that "type" is default to "text", and it will HTML-encode the value automatically. And the type "ntext" will convert "\n" to "<br />".

CDetailView::attributes CFormatter

Even if softark already gave you a good answer, I’d like to advice a but about CDetailView.

As this is a really simple widget, wich accomplish a very simple task (display items of a model) consider that many function are not available, so is normal discard at all this widget and write his own html.

In all my works I always use CGridView and CListView, but CDetail view I am nearly always forced to discard because the html needed is not a table.

Thanks to both of you, the information has helped a ton! I appreciate it.