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

4

9

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());
Pangaro answered 10/10, 2012 at 4:41 Comment(0)
M
20

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.

Mortmain answered 10/10, 2012 at 7:31 Comment(3)
@Kannan, It's cool too :) more effectivly I think. I wrote my ExtHtml 2 year ago.Proud
Yeah this is so fast and simple, and now am confused to select best answer.. :) anyway thanks @MortmainPangaro
after some experiment realized that this method is suitable to my changed scenario. And for general this is apt.. so I'm accepting this.Pangaro
P
7

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;
}
Proud answered 10/10, 2012 at 4:51 Comment(2)
wow!!.. thank u @Sergey, me following the second way,.. thanks a lotPangaro
@Kannan, in 2nd way : see if (is_callable($textField)) $text=call_user_func($textField, $model); - You can use it for get Many variants with callback functionProud
F
0

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; });
    }
Frisette answered 3/2, 2015 at 12:26 Comment(0)
F
0

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; });
 }
Fritzfritze answered 17/12, 2015 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.