M
M
Mikha Pankratov2015-12-07 21:27:49
Yii
Mikha Pankratov, 2015-12-07 21:27:49

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]);

trying to save - when getting data
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]);
                }

But the trouble is that it saves me the same data in 2 lines - where I have type=1 and type=2. How can I need data for each line of my own)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander N++, 2015-12-07
@sanchezzzhak

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; ?>

Then everything is as instructed
if (Model::loadMultiple($models , Yii::$app->request->post()) && 
        Model::validateMultiple($models )) {
... }

A
Andrew, 2015-12-07
@R0dger

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 question

Ask a Question

731 491 924 answers to any question