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

11.5k views Asked by At

I have a dropDownList in my view, it is populating from clients table, the table contains columns like first_name, last_name,id etc., Now I want to show the first_name and last_name as display text and id as value in drop down list, I'm done with id as value and first_name as display text, but here I want to combine those columns (first_name and last_name) and use as display text.

in model

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

in view

echo $form->dropDownList($model,'client_id',$model->getClients());
4

There are 4 answers

3
dInGd0nG On BEST ANSWER

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.

2
Sergey On

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;
}
0
MLFR2kx On

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; });
    }
0
Dmitriy Chornenkiy On

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