Monday 3 March 2014

Get concatenated name in Yii dropDownList

If you have two fields like firstname and lastname in your table then sometime you need to show concatenated name in dropDownList that is name with first name and last name. Following is the post where you will find how to deal with this in Yii Framework.

First of all you need to create method in your model.

Model (User in this case)

<?php
public function getConcatened(){
    return $this->firstname . '  ' . $this->firstname;
}
?>

Then call that function from CHtml list data by passing function name as a third parameter.

In drop down list:

<?php
$list = CHtml::listData(User::model()->findAll(array('order' =>     'firstname')), 'id', 'concatened');
echo $form->dropDownListRow($model, 'id', $list);
?>

No comments:

Post a Comment