Sql Query Result In Cdetailview

How can I print sql query in CDetailView?


what is the type of sql query?

Is custom or return a model (CActiveRecord)?

Also has a constant number of fileds or not?

It will be something like this

SELECT GROUP_CONCAT(`groupID` SEPARATOR ',') FROM `tbl_groupassign` WHERE `memberID` = 'check1' GROUP BY `memberID`;

groupId column will be printed.

You have to make an array with specific attributes (from query result)

$arr = array(array('label'=>'label A', 'value'=>'value 1'),array('label'=>'label B', 'value'=>'value B'));

after of that you want this code

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

'data' => array(), //to avoid error

'attributes' => $arr,


I want if it okay to something like this

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






         attribute = array(

          .... ),

$connection = Yii::app()->db;

$command = $connection->createCommand('select * your table');

$row = $command->queryRow();

$res = array();

    foreach ($row as $key=>$val) {

        $res[] = array('label'=>$key,'value'=>$val);


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

'data' => array(), //to avoid error

'attributes' => $res,


If you want more details about it please check


Thanks for your reply. I have done it something similiar

public static function findAllGroups() 	{

$connection = Yii::app()->db; 		

$command = $connection->createCommand() 		



->where('memberID = :uname', array(':uname' => Yii::app()->user->name)) 					->queryRow(); 		

$res = array(); 		

foreach ($command as $key=>$val)  		

{ $res[] = array('label'=>$key,'value'=>$val); 		


return $res; 	}

<?php $this->widget('zii.widgets.CDetailView', array( 'data'=>array(), 	'attributes'=>$user->findAllGroups

But this will print only the first row what if I want print all the rows?

CDetailView is appropriate for only one row.

If you want to show multiple rows it is better to use CGridview

if you have specific reason to do that in CDetailview how you want to display multiple rows ? in one Cdetailview or multiple (as rows exist) ?

I wanted to print the column that is returned by the sql query in single row with each row element separated by comma. group1, group2, group3 like this.

So, why didn’t use CGridView?

But cgridview will print it in column format and I want print in single line with comma delimiter

could you please give us a simple example what you want and how to display?

for example

table records

    id, column1, column2, column3

record 1 val1 val2 val3

record 2 val4 val5 val6

record 3 val7 val8 val9


1 val1,val2,val3…

yes normal sql query gives





But I want to print group1, group2, group3.


$connection = Yii::app()->db;

$command = $connection->createCommand('select group_id your_table');

$rows = $command->queryAll();

$concat = '';

    foreach ($rows as $row) {

        $concat = $concat .  $row['group_id'] . ',';


$res = array('label'=>'group id','value'=> $concat);

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

'data' => array(), //to avoid error

'attributes' => $res,


A big thanks KonApaz. With some modification it is working fine now.

Weclome @prat and thank you for your vote :)

could you post your modifications?

Maybe it helps other Yii members :)