如何在 Yii2 中为相关模型创建插入和更新操作的表单?
How to create a form for insert and update action for related model in Yii2?
我有两个模型:S
、Sp
。在 Sp
模型中,存在 hasOne()
与 S
的关系。
在SpController
我有两个动作,insert
和update
,如下:
public function actionCreate()
{
$model = new Sp();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
和
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
在 sp/views/_form.php
中,我有一个与 S
相关的字段,如下所示:
<?= $form->field($model->s, 'name')->textInput(['maxlength' => true]) ?>
由于关系,它在更新操作中工作正常,但在创建操作中抛出 <?= $form->field($model->s, 'name')->textInput(['maxlength' => true]) ?>
上 s
不存在的错误。
如何在创建操作中绑定 name
字段?
如果你想通过这种方式使用关系模型,你需要手动创建它。不要忘记实际保存来自 S
模型的数据。
public function actionCreate() {
$model = new Sp();
$model->populateRelation('s', new S());
if (
$model->load(Yii::$app->request->post()) && $model->validate()
&& $model->s->load(Yii::$app->request->post()) && $model->s->validate()
) {
$model->s->save();
$model->s_id = $model->s->id;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
但是你真的应该考虑创建专用的表单模型而不是直接使用 Active Record。它将使视图和控制器更加简单。
我认为实现您的目标的正确方法是创建一个具有您需要的所有属性的 FormModel(在本例中为 S 对象的 'name'),并在视图中使用它,例如:
$form->field($formModel, 'sName')->textInput(['maxlength' => true]) ?>
我有两个模型:S
、Sp
。在 Sp
模型中,存在 hasOne()
与 S
的关系。
在SpController
我有两个动作,insert
和update
,如下:
public function actionCreate()
{
$model = new Sp();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
和
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
在 sp/views/_form.php
中,我有一个与 S
相关的字段,如下所示:
<?= $form->field($model->s, 'name')->textInput(['maxlength' => true]) ?>
由于关系,它在更新操作中工作正常,但在创建操作中抛出 <?= $form->field($model->s, 'name')->textInput(['maxlength' => true]) ?>
上 s
不存在的错误。
如何在创建操作中绑定 name
字段?
如果你想通过这种方式使用关系模型,你需要手动创建它。不要忘记实际保存来自 S
模型的数据。
public function actionCreate() {
$model = new Sp();
$model->populateRelation('s', new S());
if (
$model->load(Yii::$app->request->post()) && $model->validate()
&& $model->s->load(Yii::$app->request->post()) && $model->s->validate()
) {
$model->s->save();
$model->s_id = $model->s->id;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
但是你真的应该考虑创建专用的表单模型而不是直接使用 Active Record。它将使视图和控制器更加简单。
我认为实现您的目标的正确方法是创建一个具有您需要的所有属性的 FormModel(在本例中为 S 对象的 'name'),并在视图中使用它,例如:
$form->field($formModel, 'sName')->textInput(['maxlength' => true]) ?>