2017-04-11 33 views
0

您好我添加下拉列表到我的_form之前,我仍然可以插入并提交数据到数据库,但当我用dropdownlist替换我的输入。一切正常,但当我点击提交按钮没有显示错误,但没有数据插入到数据库。如何解决这个问题?帮我pleaase即时通讯pleasee家伙..谢谢你洙多..提前.. This is the codeYii2 dropDownList - 不能将数据插入数据库

这是我Tblregion模型

<?php 

namespace backend\models; 

use Yii; 

/** 
* This is the model class for table "tbl_region". 
* 

*/ 
class TblRegion extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'tbl_region'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['nregion_id'], 'required'], 
      [['nregion_id'], 'integer'], 
      [['cregion_procode', 'cregion_proaddress'], 'string', 'max' => 40], 
      [['cregion_proname', 'cregion_prohead', 'cregion_tel', 'cregion_position', 'cregion_name'], 'string', 'max' => 50], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'cregion_procode' => 'Cregion Procode', 
      'cregion_proname' => 'Cregion Proname', 
      'cregion_prohead' => 'Cregion Prohead', 
      'cregion_proaddress' => 'Cregion Proaddress', 
      'cregion_tel' => 'Cregion Tel', 
      'cregion_position' => 'Cregion Position', 
      'cregion_name' => 'Cregion Name', 
      'nregion_id' => 'Region', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getTblProvinces() 
    { 
     return $this->hasMany(TblProvince::className(), ['nregion_id' => 'nregion_id']); 
    } 
} 


<?php 

namespace backend\models; 

use Yii; 

/** 
* This is the model class for table "tbl_province". 
*/ 
class TblProvince extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'tbl_province'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['nregion_id', 'cprovince_name'], 'required'], 
      [['nregion_id'], 'integer'], 
      [['cprovince_name'], 'string', 'max' => 50], 
      [['cprovince_areacode'], 'string', 'max' => 10], 
      [['cprovince_code'], 'string', 'max' => 40], 
      [['nregion_id'], 'exist', 'skipOnError' => true, 'targetClass' => TblRegion::className(), 'targetAttribute' => ['nregion_id' => 'nregion_id']], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'nregion_id' => 'Region Name', 
      'cprovince_name' => 'Province Name', 
      'cprovince_areacode' => 'Province Areacode', 
      'cprovince_code' => 'Province Code', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getTblCities() 
    { 
     return $this->hasMany(TblCity::className(), ['cprovince_name' => 'cprovince_name']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getTblFacilities() 
    { 
     return $this->hasMany(TblFacility::className(), ['cprovince_name' => 'cprovince_name']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getregion() 
    { 
     return $this->hasOne(TblRegion::className(), ['nregion_id' => 'nregion_id']); 
    } 
} 
+0

显示'tblregion'模型。 – Bizley

+0

@Bizley这里是模型。谢谢 – DULF

回答

0

我不知道这里有什么确切的问题,但你可以检查几件事情:

  • 始终使用准确的名字 - 当模型类的名字是TblRegion使用TblRegion而不是tblregion; Windows不区分大小写,但Linux系统。
  • 什么模式,你使用这种形式的工作你还没有提及,但基于该字段的名字,我想这是TblProvince - 场region不包括在这个模型中,但您填写的下拉数据:nregion_id =>cregion_name所以最有可能,你应该重新命名这个领域:

    <?= $form->field($model, 'nregion_id')->dropDownList(
        ArrayHelper::map(TblRegion::find()->all(), 'nregion_id', 'cregion_name'), 
        ['prompt' => 'Select Region'] 
    ) ?> 
    
+0

没有。我的意思是这个区域来自于模型TblProvince,它具有函数public function getregion() {$ this-> hasOne(TblRegion :: className(),['nregion_id'=>'nregion_id']); } – DULF

0

答案。

<?= $form->field($model, 'nregion_id')->dropDownList(
ArrayHelper::map(tblregion::find()->asArray()->all(),'nregion_id','nregion_id'), 
['prompt'=>'Select Region'])?> 

我的错误是我使用区域而不是nregion_id。我也加了代码->asArray()

谢谢你们..