Records neither updated nor created/multimodel

[color="#242729"][font=“Arial,”]I have tow mayor problems: MultiModels won’t be validated, so Records won’t be updated or created. Controller logic for updating and creating is similar. Let’s begin with updating. I notice, that $model_person will not be loaded with records. Furthermore, I will get following feedback:[/font][/color]

[color="#242729"]


Data is already expired

[/color][color="#242729"][font="Arial,"]for $model_person.[/font][/color]

[color="#242729"]


[var_dump($model);

var_dump($model_person);

[/color][font="Arial,"][size="2"]is all right. No abnormalities noticeable.[/size][/font]

[font="Arial,"][size="2"]Any ideas, what is wrong with this code?[/size][/font]





    public function actionUpdate($id) {

 		//try { removed,until problem has been solved

            $behavior = new \common\wsl_components\Wsl_Blameable_Behavior();

            $aktualisiert_von = $behavior->get_User_Person_Id();

            $model = $this->findModel($id);

            //var_dump($model);

            $model->scenario = 'create';

            $model_person = Person::findOne($model->id_person);

            //var_dump($model_person);

            $model_person->scenario = 'create';

            if (!$model || !$model_person) {

                throw new NotFoundHttpException("Mindestens einer der beiden Tabellen sind korrupt");

            }

            $model->aktualisiert_von = $aktualisiert_von;

            $model_person->aktualisiert_von = $aktualisiert_von;

            if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) {

                $valid=$model->validate();

                $isValid=$model_person->validate();

		$isValid=$valid && $isValid;

                if ($isValid) {

                    $model->avatar = UploadedFile::getInstances($model, 'avatar');

                    $model->upload();

                    $model->saveAll(false);

                    $model_person->saveAll(false);

                    return $this->redirect(['view', 'id' => $model->id]);

                } else {

                    $error_person = $model_person->getErrors();

                    $error_bewerber=$model->getErrors();

                    foreach ($error_person as $values) {

                    	foreach ($values as $ausgabe) {

                        	echo"<p>" . $ausgabe . "</p>";

                        }

                    }

                    foreach ($error_bewerber as $values) {

                    	foreach ($values as $ausgabe) {

                        	echo"<p>" . $ausgabe . "</p>";

                        }

                    }

                    return $this->render('update', [

                                'model' => $model,

                                'model_person'=>$model_person

                    ]);

                }

                //solange noch kein Submittbutton gepusht

            } else {

                return $this->render('update', [

                            'model' => $model,

                            'model_person'=>$model_person

                ]);

            }

/*

removed,until problem has been solved

        } catch (\Exception $error) { //fange den schweren MySQL-Fehler ab

            $go_back = "bewerber";

            \common\wsl_components\error_handling::error($error, $go_back);

        }

*/

    }



Here is _form.php,which will be renderd in both cases, updating or creating





<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use kartik\date\DatePicker;

use \common\modules\basis\models\Person;

use common\modules\lookup\models\LPersonArt;

use kartik\widgets\FileInput;


/*

  26.09.2017/tklustig/Die folgenden Anweisungen übergeben an die jeweiligen Variablen jenen FK, der

  in der Tabelle Person hinterlegt ist. Sofern der FK nicht vorhanden ist,wird eine Exception ausgeworfen.Deshalb muss in dem CRUD der Tabelle

  l_person_art dafür gesorgt werden,daß Bewerber=PK(1) und Mitarbeiter=PK(3) haben!

 */

$id_person_art_bewerber = Person::find()->where(['id_person_art' => 1])->one()->id_person_art;

$id_person_art_mitarbeiter = Person::find()->where(['id_person_art' => 3])->one()->id_person_art;

?>


<!-- START ACCORDION & CAROUSEL-->

<div class="row">

    <div class="col-md-6">

        <div class="box box-solid">

            <div class="box-header with-border">

                <h3 class="box-title">Aditional tables(label_1) </h3>

            </div>

            <!-- /.box-header -->

            <div class="box-body">

                <div class="box-group" id="accordion">

                    <!-- we are adding the .panel class so bootstrap.js collapse plugin detects it -->

                    <div class="panel box box-primary">

                        <div class="box-header with-border">

                            <h4 class="box-title">

                                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">

                                    Inputfelder der Tabelle person(label_2)

                                </a>

                            </h4>

                        </div>

                        <div id="collapseOne" class="panel-collapse collapse in">

                            <div class="box-body">

                                <div class="person-form">


                                    <?php

                                    $form = ActiveForm::begin([

                                                'id' => 'bewerber-update-form',

                                                'options' => ['enctype' => 'multipart/form-data',

                                                'class' => 'person-form']

                                    ]);

                                    ?>

                                    <?= $form->errorSummary($model); ?>

                                    <?= $form->errorSummary($model_person); ?>

                                    <?=																				

   								... more inputfields of same type see below ...


                                    <?=

                                    $form->field($model_person, 'id_personentyp_defizit')->widget(\kartik\widgets\Select2::classname(), [

                                        'data' => \yii\helpers\ArrayHelper::map(\common\modules\lookup\models\LPersonentyp::find()->orderBy('id')->asArray()->all(), 'id', 'id'),

                                        'options' => ['placeholder' => Yii::t('app', 'Choose L personentyp')],

                                        'pluginOptions' => [

                                            'allowClear' => true

                                        ],

                                    ]);

                                    ?>

              									

   								... more inputfields of same type see below ...									


                                    <?=

                                    $form->field($model_person, 'anrede_persoenlich')->widget(\kartik\checkbox\CheckboxX::classname(), [

                                        'autoLabel' => true

                                    ])->label(false);

                                    ?>                       									

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </div>

</div>

<!-- END ACCORDION & CAROUSEL-->


<div class="bewerber-form">


    <?=

    //28.09.2017/tklustig/Initialisiert das Upload-Formular.Damit das multiple uploading klappt,muss die property als Array eingebunden werden    


    $form->field($model, 'avatar[]')->widget(FileInput::classname(), [

        'options' => ['accept' => 'image/*', 'multiple' => true]

    ]);

    ?>

	

	... more inputfields of same type see below ...

    <?=

    $form->field($model, 'id_person')->widget(\kartik\widgets\Select2::classname(), [

        'data' => Person::getPerson($id_person_art_bewerber),

        'options' => ['placeholder' => Yii::t('app', 'Bitte geben sie den Nachnamen des Bewerbers über die DropDownbox ein')],

        'pluginOptions' => [

            'allowClear' => true,

        ],

    ]);

    ?>

	

	... more inputfields of same type see below ...


    <?=

    $form->field($model, 'avgs')->widget(\kartik\checkbox\CheckboxX::classname(), [

        'autoLabel' => true

    ])->label(false);

    ?>




    <div class="form-group">

        <?php if (Yii::$app->controller->action->id != 'save-as-new'): ?>

            <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

        <?php endif; ?>

        <?php if (Yii::$app->controller->action->id != 'create'): ?>

            <?= Html::submitButton('Save As New', ['class' => 'btn btn-info', 'value' => '1', 'name' => '_asnew']) ?>

        <?php endif; ?>

        <?= Html::a(Yii::t('app', 'Cancel'), \Yii::$app->urlManager->baseUrl . '/yiic.php/bewerber', ['class' => 'btn btn-danger']) ?>

    </div>




    <?php ActiveForm::end(); ?>


</div>



[size="2"]And finally,here is updating.php[/size]

[size="2"]


<?php

use yii\helpers\Html;[/size]

$this->title = Yii::t('app', 'Update {modelClass}: ', 'modelClass' => 'Bewerber',]) . ' ' . $model->id;

$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Bewerber'), 'url' => ['index']];

$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];

$this->params['breadcrumbs'][] = Yii::t('app', 'Update');

$model_person = new common\modules\basis\models\Person();

?><div class="bewerber-update">

<h1>

<?= Html::encode($this->title) ?></h1>

<div class="search-form" style="display:none"></div>

<?=$this->render('_form', ['model_person' => $model_person,'model' => $model,])  ?>

</div>

[/size]

[color="#006400"]/* topics merged */[/color]

Please do not start a new thread with the same problem.

[EDIT]

Um, other moderator (probably @mdomba) seems to have deleted your new thread that I wanted to merge. Sorry.

Anyway, please don’t.

[EDIT]

yes, you got me @softark :D

Sorry, but I’m totally desperated, 'cause no solution avaliable.

But’s it’s okay, U deleted my thread doubled. So are rules

Check your html output of the form. Does it have a correct pair of ‘<form>’ and ‘</form>’? I guess it doesn’t.

In your view script, "ActiveForm::begin()" and "ActiveForm::end()" are not placed in the same level of the nested "div"s. This will cause a strange behavior of the form.

I changed div elements as suggested. Validating seems to be okay, now, except two:

First:

Records of $model_person still won’t bel loaded, although I should get them using this in Controller




 		$model_person = Person::findOne($model->id_person);



Second:

I still get message




Data is already expired



var_dump shows this:




C:\xampp\htdocs\yii2_perswitch\frontend\modules\bewerber\controllers\BewerberController.php:126:

object(common\modules\basis\models\Person)[246]

  public 'avatar' => null

  private '_attributes' (yii\db\BaseActiveRecord) => 

    array (size=26)

      'id' => int 1

      'id_person_art' => int 1

      'id_person_mitarbeiter' => int 1

      'id_personentyp_dominant' => int 2

      'id_personentyp_peripher' => int 1

      'id_personentyp_defizit' => int 1

      'id_familienstand' => int 4

      'id_anrede' => int 1

      'id_titel' => int 1

      'vorname' => string 'Markus' (length=6)

      'nachname' => string 'Götz' (length=5)

.

.

.



P.S.: For creating, most things are all right by now, 'cause no records are available.

So, if this last thing could be solved, I’d be very,very satisified.

Ex ante, thanks a lot for your help regarding <div>

Try to code the controller using "load" and "save" only, instead of using "loadAll" and "saveAll".

If you call "$model->saveAll", then the person record will be updated by "$model->person", not by $model_person.

And when you call “$model_person->saveAll” (or “$model_person->save”) after you’ve called “$model->saveAll”, it means that you are trying to update the person record twice. And it will fail because the version of the record has been incremented and $model_person is obsoleted. That’s why “Data is already expired”.

Doesn’t help this time. Both problem still are available

[list=1][]No records loaded for $model_person(only for $model)[]I still get message:" Data [color="#000088"][size="2"]is[/size][/color][size="2"] already expired"[/size][/list]

[size="2"]I code controller like this:[/size]

[size="2"]




if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) {

$valid=$model->validate();

$isValid = $model_person->validate();

$isValid=$valid&$isValid;

]if ($isValid) {

$model->avatar = UploadedFile::getInstances($model, 'avatar');

$model->upload();

$model->save(); 

$model_person-save();

return $this->redirect(['view', 'id' => $model->id]);

} else {

.

.

.



[/size]

[size="2"]Proplem is Validating, not saving…[/size]

[size=“2”]Maybe, it’s because of updating.php(see above). There, i have to instance an object of class in order to get $model_person rendered. I don’t have to do this with $model. What do you think?[/size]

Do not create an instance of Person here.

Bingo. That’s it. This thread was a really hard issue, which never would have been solved without ur help.

[color=#3C3C3C][font=Arial, sans-serif]

[/font][/color]

[color=#3C3C3C][font=Arial, sans-serif]A[/font][/color][font=Arial, sans-serif] thousand thanks for this![/font]

[b][font=Arial, sans-serif]

[/font][/b]

[size=2]U rescued me in some respects.[/size]

[size=2]P.S.: Instead of instancing in update.php, I instanced object in index.php…[/size]

[size=2]

[/size]

[size=2]Happy Coding. [/size]

[size=2]This thread can be finished as solved![/size]