我有一个模型Taluka,我们应该选择区域并输入尽可能多的talukas。
但问题是即使一些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(false);
}
}
}
return $this->redirect(['index']);
}
else {
return $this->render('create', [
'model' => $model,
]);
}
}