我有taluka模型选择地区,并且输入尽可能多的talukas。 但记录没有得到保存。
public function actionCreate()
{
$model = new Taluka();
if ($model->load(Yii::$app->request->post()) ) {
$talukaslist = $model->talukas;
if(is_array($talukaslist))
{
foreach($talukaslist as $taluka)
{
if($taluka ==null)
{
return $this->render('create', [
'model' => $model,
]);
}
else
if($taluka!=null)
{
$talukaRecord = new Taluka();
$talukaRecord->DistrictId = $model->DistrictId;
$talukaRecord->Taluka = $taluka;
$talukaRecord->save();
}
}
}
return $this->redirect(['index']);
}
else {
return $this->render('create', [
'model' => $model,
]);
}
}
如果我使用$ talukaRecord-> save(false),那么即使验证失败,记录也会被保存。
型号:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "taluka".
*
* @property integer $TalukaId
* @property integer $DistrictId
* @property string $Taluka
*
* @property Area[] $areas
* @property Colony[] $colonies
* @property Colonydemographic[] $colonydemographics
* @property District $district
* @property Village[] $villages
* @property Villagedemographic[] $villagedemographics
*/
class Taluka extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public $talukas=[];
public static function tableName()
{
return 'taluka';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['DistrictId', 'Taluka'], 'required'],
[['DistrictId'], 'integer'],
[['talukas'], 'required'],
[['Taluka'], 'string', 'max' => 100],
[['DistrictId'], 'exist', 'skipOnError' => true, 'targetClass' => District::className(), 'targetAttribute' => ['DistrictId' => 'DistrictId']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'TalukaId' => 'Taluka ID',
'DistrictId' => 'District',
'talukas' => 'Taluka',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAreas()
{
return $this->hasMany(Area::className(), ['TalukaId' => 'TalukaId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getColonies()
{
return $this->hasMany(Colony::className(), ['TalukaId' => 'TalukaId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getColonydemographics()
{
return $this->hasMany(Colonydemographic::className(), ['TalukaId' => 'TalukaId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getDistrict()
{
return $this->hasOne(District::className(), ['DistrictId' => 'DistrictId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVillages()
{
return $this->hasMany(Village::className(), ['TalukaId' => 'TalukaId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVillagedemographics()
{
return $this->hasMany(Villagedemographic::className(), ['TalukaId' => 'TalukaId']);
}
}
答案 0 :(得分:0)
请尝试以下代码
public function actionCreate()
{
$model = new Taluka();
if ($model->load(Yii::$app->request->post()) ) {
$talukaslist = $model->talukas;
if(isset($talukaslist) && && $talukaslist!=array())
{
foreach($talukaslist as $taluka)
{
if(isset($taluka) && $taluka ==null)
{
return $this->render('create', [
'model' => $model,
]);
}
else
if(isset($taluka) && $taluka!=null)
{
$talukaRecord = new Taluka();
$talukaRecord->DistrictId = $model->DistrictId;
$talukaRecord->Taluka = $taluka;
if($talukaRecord->save())
{}
else
{
echo "<pre>";
print_r($talukaRecord->getErrors());die;
}
}
}
}
return $this->redirect(['index']);
}
else {
return $this->render('create', [
'model' => $model,
]);
}
}