dsafew1231
dsafew1231
2017-01-18 10:58

在yii 1中的下拉列表中保存另一个字段值而不是id

已采纳

I have following code in my view

<?php echo $form->dropDownList($model,'p_28',
    CHtml::listData(AdCourt::model()->findAll(),'id','name') ,
    array(
        'class'=>'form-control',
        'style'=>'width:100%;',

        'empty'=>Yii::t('plaint','Суд турини танланг'),
        'ajax' => array(
            'type'=>'POST', //request type
            'url'=>$this->createUrl('getcourts'),
            'update'=>'#ABlockForm_p_29',
        )
    )); ?>

AdCourt takes values from ad_court table. This tables consists of id, name and court_id. I need to save database court_id instead of id.

How can I do it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzhuo1958 dongzhuo1958 4年前

    The second parameter of listData is the attribute of the items that will be used as the value for the <option> tag. As such to use the court_id instead of the id you should pass it instead:

    CHtml::listData(AdCourt::model()->findAll(),'court_id','name')
    
    点赞 评论 复制链接分享