Concatenate 2 columns into Dropdown list

Hello, this is my first topic on Yii…

I will appreciate very much if someone can help me with this problem:

Is there a chance to concatenate 2 table columns (first name and last name) in a dropdown list so that the result displays as an option? The dropdown list takes the data from another model which is related to it.

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'table2_id'); ?&gt;

	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model, 'table2_id', CHtml::listData(Table2::model()-&gt;findAll(), 'id', 'lastname'),array('prompt' =&gt; 'Select')); ?&gt;

	&lt;?php echo &#036;form-&gt;error(&#036;model,'table2_id'); ?&gt;


I have tried to pass multiple parameters but it doesn´t work.

Any suggesions?


recreate the array yourself:

$models = Table2::model()->findAll();

$data = array();

foreach ($models as $model)

    $data[$model->id] = $model->lastname . ' '. $model->secondAttribute;     

echo $form->dropDownList($model, 'table2_id', $data ,array('prompt' => 'Select'));

Nice… Thanks Antonio.

No worries… glad to help



    return $this->first_name.' '.$this->last_name;



i am using yii2 i followed your instructions but this is giving error

here is my code


    $models = Size::model()->findAll();

	$data = array();

	foreach ($models as $model)

		$data[$model->fld_id] = $model->fld_width . ' '. $model->fld_height;     

	echo $form->dropDownList($model, 'fld_size_id', $data ,array('prompt' => 'Select'));


Here’s the approach I took. I think the latest api supports a custom function.


		$myList = CHtml::listData(MyTable::model()->findAll(), 'id', function($data){

			return "({$data->start_date}-{$data->end_date}) {$data->name}";


		echo $form->dropDownList($model,'foreign_id', $myList); ?>

Gracias me ha servido mucho tu aporte, Thanks