Drop down menu yii dropdownList是否显示相同的值?

Drop down menu yii dropdownList是否显示相同的值?,drop-down-menu,yii,Drop Down Menu,Yii,我在项目中的前任没有将下拉列表值保存在表中;它们在html文件中…这家伙也没有使用$form->dropdownList()来创建选择 当然,我现在在编辑时预先选择值时遇到了巨大的问题;因此,我将所有更改为$form->dropdownList() 但现在我有一个不同的问题,就像 echo $form->dropdownList($model,'location', array("Art","Gallery","Bar","Club")); 现在为数据库生成整数值 我知道

我在项目中的前任没有将下拉列表值保存在表中;它们在html文件中…这家伙也没有使用
$form->dropdownList()
来创建选择

当然,我现在在编辑时预先选择值时遇到了巨大的问题;因此,我将所有
更改为
$form->dropdownList()

但现在我有一个不同的问题,就像

echo $form->dropdownList($model,'location',
        array("Art","Gallery","Bar","Club"));
现在为数据库生成整数值

我知道我可以这样设置显示值: 数组(“艺术”=>“艺术”)。。。。 但我希望避免这种情况-有一堆视图直接显示值…:(


有没有办法告诉yii DB值应与显示值相同?

您可以覆盖
CActiveForm
小部件的
dropDownList
方法,如下所示:

<?php
class ActiveForm extends CActiveForm
{
    public $valuesAsKeys = false;

    public function dropDownList($model,$attribute,$data,$htmlOptions=array())
    {
        if (!$this->valuesAsKeys)
            return parent::dropDownList($model, $attribute, $data, $htmlOptions);

        $newData = array();
        foreach ($data as $value)
            $newData[$value] = $value;
        return parent::dropDownList($model, $attribute, $newData, $htmlOptions);
    }
}

您可以覆盖
CActiveForm
小部件的
dropDownList
方法,如下所示:

<?php
class ActiveForm extends CActiveForm
{
    public $valuesAsKeys = false;

    public function dropDownList($model,$attribute,$data,$htmlOptions=array())
    {
        if (!$this->valuesAsKeys)
            return parent::dropDownList($model, $attribute, $data, $htmlOptions);

        $newData = array();
        foreach ($data as $value)
            $newData[$value] = $value;
        return parent::dropDownList($model, $attribute, $newData, $htmlOptions);
    }
}

如果数组值是唯一的(Yii需要唯一的键),则可以使用

$data = array("Art","Gallery","Bar","Club");
echo $form->dropdownList($model,'location', array_combine($data, $data));

array\u combine
将对listdata的键和值使用相同的数据。

如果数组值是唯一的(Yii需要唯一的键),则可以使用

$data = array("Art","Gallery","Bar","Club");
echo $form->dropdownList($model,'location', array_combine($data, $data));

array\u combine
将对listdata的键和值使用相同的数据。

Harry B的答案非常有效,谢谢!在我的例子中,我想使用自定义选项值并包含提示,因此为了扩展他的示例,我创建了一个返回键和值数组的方法。键数组以“prompt”开头“values”数组以“(select)”开头,然后我循环我的数据源,将每个键添加到keys数组中,将每个值添加到values数组中。我的代码有点复杂,涉及正则表达式,但基本要点如下:

// Controller method popupRowListValues($source) $displayKeys = array('prompt'); $displayValues = array('select'); foreach ($source as $key=>$value) { $displayKeys[] = $key; $displayValues = $value; } // View $displayList = $this->popupRowListValues($source); echo $form->dropDownListRow($model, $column_name, array_combine( $displayList['displayKeys'], $displayList['displayValues'] )); //控制器方法PopupUpWListValues($source) $displayKeys=array('prompt'); $displayValues=array('select'); foreach($sourceas$key=>$value){ $displayKeys[]=$key; $displayValues=$value; } //看法 $displayList=$this->popuroupWListValues($source); echo$form->dropDownListRow($model,$column\u name,array\u combine)( $displayList['displayKeys'],$displayList['displayValues'] ));
Harry B的答案很好,谢谢!在我的例子中,我想使用自定义选项值,还包括一个提示符,因此为了扩展他的示例,我创建了一个返回键和值数组的方法。键数组以“提示符”开头,“值”数组以“(选择)开头'然后我循环我的数据源,将每个键添加到键数组中,将每个值添加到值数组中。我的代码有点复杂,涉及正则表达式,但基本要点如下:

// Controller method popupRowListValues($source) $displayKeys = array('prompt'); $displayValues = array('select'); foreach ($source as $key=>$value) { $displayKeys[] = $key; $displayValues = $value; } // View $displayList = $this->popupRowListValues($source); echo $form->dropDownListRow($model, $column_name, array_combine( $displayList['displayKeys'], $displayList['displayValues'] )); //控制器方法PopupUpWListValues($source) $displayKeys=array('prompt'); $displayValues=array('select'); foreach($sourceas$key=>$value){ $displayKeys[]=$key; $displayValues=$value; } //看法 $displayList=$this->popuroupWListValues($source); echo$form->dropDownListRow($model,$column\u name,array\u combine)( $displayList['displayKeys'],$displayList['displayValues'] ));
@哈利B的答案更合适。@哈利B的答案更合适。