2013-03-27 47 views
0

我在yii中使用了一个解析api的组件。所以在表单下拉列表中调用列表国家的功能。该函数返回国家列表作为数组。 form.php的在yii中没有模型的下拉列表

<?php echo $form->labelEx($model,'country'); ?> 
<?php $cty= Country::getCountry(); 
     echo $form->dropdownList($model,'country', $cty , array('style'=>'width: 175px','empty'=>array('empty'=>Yii::t('app','Select Country'))));?> 

现在下降正确装入全国上下的名单,但是当节省时间国家的相应的ID被保存。我想在国家名称保存db.How它解决了吗?

+1

是什么'Country'看起来像您可以用这种方式?为什么要保存这个名字,如果一个国家的主键是它的ID? – Jon 2013-03-27 11:53:16

+0

<?php $ new = Country :: getCountry(); ($ new as $ strn) { print_r($ new); echo“
”; } ?> 它是一个组件函数,返回国家列表作为数组。像这样的阵列输出([0] =>阿富汗[1] =>奥兰群岛[2] =>阿尔巴尼亚[3] =>阿尔及利亚[4] =>美属萨摩亚 – Aneesh 2013-03-27 12:27:36

回答

2

您必须使用所需的键/值构建您自己的自定义数组,例如, :

$cty = Country::getCountry(); 
$cty = array_combine(array_values($cty), $cty); 
0

(如果你需要国家表中的所有项目的情况下)

$cty = CHtml::listData(Country::model()->findAll(), 'name', 'name');