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

隐身守侯 提交于 2019-12-03 03:06:01

Heres another method In 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 model

function 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; });
    }

Previous comment with "compact mode" with anonymous function has an error! Right code is:

 function getClients()
 {
    $clients = Client::model()->findAll();
    return CHtml::listData($clients, 'id', function($clients) { return $client->first_name . ' ' . $client->last_name; });
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!