Answer the question
In order to leave comments, you need to log in
Saving 2 rows in one table?
Hello, I got this problem. I generate 2 models
$model1 = table::findOne(['id' => 1, 'type' => 1]);
$model2 = table::findOne(['id' => 1, 'type' => 2]);
if (isset($model1) && $model1->load(Yii::$app->request->post()) && $model1->validate()) {
$model1->save(true, ['type' => 1]);
}
if (isset($model2) && $model2->load(Yii::$app->request->post()) && $model2->validate()) {
$model2->save(true, ['type' => 2]);
}
Answer the question
In order to leave comments, you need to log in
Well, you have the same form name, you need to make multidimensional fields
www.yiiframework.com/wiki/666/handling-tabular-dat...
$models = table::findAll['id' => 1, 'type' => [1 ,2]]);
<?php foreach($models as $i=>$item): ?>
<?= $form->field($item,"[$i]name")->textInput(['maxlength' => 32]); ?>
<?= $form->field($item,"[$i]price"); ?>
<?= $form->field($item,"[$i]count"); ?>
<?= $form->field($item,"[$i]description"); ?>
<?php endforeach; ?>
if (Model::loadMultiple($models , Yii::$app->request->post()) &&
Model::validateMultiple($models )) {
... }
but show the View where you display the form and show what POST sends you .. I think the problems will be there ...
PS: I hope you load these models in the action where you save ... and you just need to specify $model1->save()
i.e. to. the data you assigned above is already in $model1->load(Yii::$app->request->post())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question