【问题标题】:In Yii framework how can I Combine columns and show as display string in dropdownlist在 Yii 框架中,如何组合列并在下拉列表中显示为显示字符串
【发布时间】:2012-10-10 04:41:38
【问题描述】:

在我看来我有一个dropDownList,它是从clients 表中填充的,该表包含first_namelast_nameid 等列,现在我想显示first_namelast_name 作为显示文本和id 作为下拉列表中的值,我完成了id 作为值和first_name 作为显示文本,但在这里我想组合这些列(first_name 和@ 987654332@) 并用作显示文本。

在模型中

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

可见

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

【问题讨论】:

    标签: php yii html.dropdownlistfor virtual-attribute yii-cmodel


    【解决方案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 虚拟属性,还可以在需要全名的任何地方使用。

    【讨论】:

    • @Kannan,这也很酷 :) 我认为更有效。我在 2 年前写了我的 ExtHtml。
    • 是的,这又快又简单,现在很困惑选择最佳答案.. :) 无论如何感谢@dInGd0nG
    • 经过一些实验后发现这种方法适合我改变的场景。总的来说,这很合适..所以我接受了。
    【解决方案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;
    }
    

    【讨论】:

    • 哇!!.. 谢谢@Sergey,我遵循第二种方式,.. 非常感谢
    • @Kannan,第二种方式:查看是否 (is_callable($textField)) $text=call_user_func($textField, $model); - 你可以用它来获取许多带有回调函数的变体
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      之前带有匿名函数的“紧凑模式”的评论有错误! 正确的代码是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 2015-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        相关资源
        最近更新 更多