Yii2 dropDownList - 无法将数据插入数据库
Yii2 dropDownList -Cannot insert data into database
您好,我之前将下拉列表添加到我的 _form 中,我仍然可以将数据插入和提交到数据库中,但是当我用下拉列表替换我的输入时。一切正常,但是当我点击提交按钮时,没有显示任何错误,但是没有数据插入 database.How 我可以解决这个问题吗?请帮帮我..非常感谢..提前..
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']);
}
}
我不确定这里的确切问题是什么,但您可以检查几件事:
- 始终使用准确的名称 - 当模型 class 名称为
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']
) ?>
答案是。
<?= $form->field($model, 'nregion_id')->dropDownList(
ArrayHelper::map(tblregion::find()->asArray()->all(),'nregion_id','nregion_id'),
['prompt'=>'Select Region'])?>
我的错误是我使用区域而不是 nregion_id
。
我也添加代码->asArray()
。
谢谢大家..
您好,我之前将下拉列表添加到我的 _form 中,我仍然可以将数据插入和提交到数据库中,但是当我用下拉列表替换我的输入时。一切正常,但是当我点击提交按钮时,没有显示任何错误,但是没有数据插入 database.How 我可以解决这个问题吗?请帮帮我..非常感谢..提前.. 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']);
}
}
我不确定这里的确切问题是什么,但您可以检查几件事:
- 始终使用准确的名称 - 当模型 class 名称为
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'] ) ?>
答案是。
<?= $form->field($model, 'nregion_id')->dropDownList(
ArrayHelper::map(tblregion::find()->asArray()->all(),'nregion_id','nregion_id'),
['prompt'=>'Select Region'])?>
我的错误是我使用区域而不是 nregion_id
。
我也添加代码->asArray()
。
谢谢大家..