In Yii framework how can I Combine columns and show as display string in dropdownlist In Yii framework how can I Combine columns and show as display string in dropdownlist php php

In Yii framework how can I Combine columns and show as display string in dropdownlist


Heres another methodIn model

function getFullName(){    return $this->first_name.' '.$this->last_name;}

and

function getClients(){    $Clients = Client::model()->findAll();    $list    = CHtml::listData($Clients , 'client_id', 'fullName');    return $list;}

I think this is kinda reusable method since you can use the fullName virtual attribute not only in dropdownlist but everywhere a full name is needed.


1st variant (Easy):

$list = array();foreach ($Clients as $c) {    $list[$c->id] = $c->first_name . ' ' . $c->last_name;}

2nd variant (Universal):

class ExtHtml extends CHtml {    public static function listData($models,$valueField,$textField,$groupField='')    {        $listData=array();        if($groupField==='')        {            foreach($models as $model)            {                $value=self::value($model,$valueField);                if (is_array($textField)) {                    $t = array();                    foreach ($textField as $field) {                        $t[]=self::value($model,$field,$field);                    }                    $text=implode(' ', $t);                } else {                    $text=self::value($model,$textField, null);                    if ($text == null) {                        if (is_callable($textField)) $text=call_user_func($textField, $model);                        else $text = $textField;                    }                }                $listData[$value]=$text;            }        }        else        {            foreach($models as $model)            {                $group=self::value($model,$groupField);                $value=self::value($model,$valueField);                if (is_array($textField)) {                    $t = array();                    foreach ($textField as $field) {                        $t[]=self::value($model,$field,$field);                    }                    $text=implode(' ', $t);                } else {                    $text=self::value($model,$textField, null);                    if ($text == null) {                        if (is_callable($textField)) $text=call_user_func($textField, $model);                        else $text = $textField;                    }                }                $listData[$group][$value]=$text;            }        }        return $listData;    }    public static function value($model,$attribute,$defaultValue=null)    {        foreach(explode('.',$attribute) as $name)        {            if(is_object($model) && ($model->hasAttribute($name) || isset($model->{$name})))                $model=$model->$name;            else if(is_array($model) && isset($model[$name]))                $model=$model[$name];            else                return $defaultValue;        }        return $model;    }}// in modelfunction getClients(){    $Clients = Client::model()->findAll();    $list    = ExtHtml::listData($Clients , 'client_id', array('first_name', 'last_name'));    return $list;}

3rd variant (Mysql)

function getClients(){    $Clients = Client::model()->findAll(array('select' => 'concat(first_name, " ", last_name) as first_name'));    $list    = CHtml::listData($Clients , 'client_id', 'first_name');    return $list;}


Try this compact mode using "anonymous function" feature of PHP:

    function getClients()    {        return CHtml::listData(Client::model()->findAll(), 'id', function($client) { return $client->first_name . ' ' . $client->last_name; });    }