Code Blog

Displaying 46-46 of 46 results.
2014/12/20 Yii

I often need to print an array in Yii CDetailView and CGridView, and this is a quite handy way to do it. By using the array_map function to apply the function call to map value to display string and later implode it to one string (display values seperated by commas).

$this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		'id',
		'title',
		'posted',
		array( 'name' => 'category',
               'type' => 'raw',
               'value' => is_array($model->category) ? implode(",", array_map(function($v) use ($model) { return $model->getCategoryOption($v); }, $model->category)) : "",
        ),
		array( 'name' => 'status',
               'type' => 'raw',
               'value' => $model->getStatusOption($model->status),
        ),
	),
)); 
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'codeblog-grid',
	'dataProvider'=>$dataprov,
	'filter'=>$model,
	'columns'=>
		...
		array( 'name' => 'category',
               'type' => 'raw',
               'value' => function($data) {
					if (is_array($data->category)) {
						echo implode(",", array_map(function($v) use ($data) { return $data->getCategoryOption($v); }, $data->category));
					}
				},
        ),