Yii2 dropDownList - 不能将数据插入数据库

时间:2017-04-11 05:34:02

标签: yii2-advanced-app

您好我将dropdownlist添加到我的_form之前我仍然可以插入数据并将数据提交到数据库中,但是当我用dropdownlist替换输入时。一切都很好但是当我点击提交按钮时没有显示错误,但没有数据插入数据库。如何解决这个问题?请帮帮我们..谢谢你们......提前.. This is the code

这是我的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']);
    }
}

2 个答案:

答案 0 :(得分:0)

我不确定这里有什么问题,但你可以检查几件事:

  • 始终使用确切名称 - 当模型类名称为TblRegion时使用TblRegion而不是tblregion; Windows不区分大小写,但Linux系统是。
  • 您还没有提到使用此表单的模型,但根据字段的名称,我认为它是TblProvince - 字段region未包含在此模型中,但您填写数据下拉列表:nregion_id =&gt; cregion_name所以很可能你应该重命名这个字段:

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

答案 1 :(得分:0)

答案是。

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

我的错误是我使用区域而不是nregion_id

我还添加了代码->asArray()

谢谢你们..