-->

在Yii框架我怎么能合并的列,显示在下拉列表显示字符串(In Yii framework how c

2019-08-01 07:28发布

我有一个dropDownList在我看来,它是由填充clients表,表中包含了诸如列first_namelast_nameid等,现在我想显示first_namelast_name作为显示文字和id在下拉列表中的值,我“M用做id为值和first_name作为显示文本,但在这里我想这些列(结合first_namelast_name )和显示文本使用。

模型

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

鉴于

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

Answer 1:

下面有另一种方法,在模型

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

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

我觉得这是有点重复使用的方法,因为你可以使用fullName不仅在下拉列表却处处需要一个全名虚拟属性。



Answer 2:

第一个变种(简易):

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

第二个变种(通用):

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

第三变型(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;
}


Answer 3:

使用PHP的“匿名函数”功能试试这个紧凑模式:

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


Answer 4:

以“精简模式”之前的评论匿名函数有错误! 正确的代码是:

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


文章来源: In Yii framework how can I Combine columns and show as display string in dropdownlist