9

dropDownListビューに があり、テーブルから入力clientsされています。テーブルには、などfirst_nameの列が含まれています。表示テキストとして およびをドロップダウン リストの値として表示したいので、値として、および表示テキストですが、ここではこれらの列 (と)を結合して表示テキストとして使用したいと考えています。last_nameidfirst_namelast_nameididfirst_namefirst_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());
4

4 に答える 4

20

モデル内の別の方法を次に示します

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ドロップダウンリストだけでなく、フルネームが必要なすべての場所で仮想属性を使用できるため、これは再利用可能な方法だと思います。

于 2012-10-10T07:31:47.957 に答える
7

最初のバリアント (簡単):

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

2 番目のバリアント (ユニバーサル):

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

3 番目の亜種 (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;
}
于 2012-10-10T04:51:48.587 に答える
0

PHP の「匿名関数」機能を使用して、このコンパクト モードを試してください。

    function getClients()
    {
        return CHtml::listData(Client::model()->findAll(), 'id', function($client) { return $client->first_name . ' ' . $client->last_name; });
    }
于 2015-02-03T12:26:44.257 に答える