Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Multiple instances of the same model

I want to get multiplie instance of the same model in my controller. I saw this wiki for Yii 1.1 and tried like that but in my code only last instance in form was acceble from controller my code is here (I commented code with error and variable values):

$model = new Person(['scenario' => 'create_update']);
$contractDate = new DatePart(); // DatePart is my own class
$contractExpirationDate = new DatePart(); // DatePart is my own class

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

    Yii::info(Yii::$app->request->post(),'test'); // only one instance of Person and one instance of DatePart are available here
    Yii::info($_POST['DatePart'],'test'); // only last instance of DatePart (contractExpirationDate in html form) is available here
    Yii::info($_POST['DatePart'][0],'test'); // Error: Undefined offset: 0
    Yii::info($_POST['DatePart'][1],'test'); // Error: Undefined offset: 1

    $model->save();
    return $this->redirect(['view', 'id' => $model->id]);
} else {
    return $this->render('create', [
        'model' => $model,
        'contractDate' => $contractDate,
        'contractExpirationDate' => $contractExpirationDate,
    ]);
}

It is my form view in _form.php:

<?php

use yii\helpers\Html;
//use yii\widgets\ActiveForm;
use kartik\widgets\ActiveForm;
use common\models\DataOperations;

/* @var $this yii\web\View */
/* @var $model app\models\Person */
/* @var $contractDate backend\viewModels\DatePart */
/* @var $contractExpirationDate backend\viewModels\DatePart */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="user-form">
    <?php
    $form = kartik\widgets\ActiveForm::begin(
        [
            'id' => $model->isNewRecord ? 'user-form-create' : 'user-form-update',
            'type' => ActiveForm::TYPE_VERTICAL,
            //'enableAjaxValidation' => true,
            'fieldConfig' => [
                //'autoPlaceholder'=>true
            ]
        ]);
    ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 60]) ?>  
    <?= $form->field($model, 'family')->textInput(['maxlength' => 60]) ?>
    <?= $form->field($model, 'mobile')->textInput() ?>

    <?= $form->field($contractDate, 'year')->textInput() ?>
    <?= $form->field($contractDate, 'month')->textInput() ?>
    <?= $form->field($contractDate, 'day')->textInput() ?>

    <?= $form->field($contractExpirationDate, 'year')->textInput() ?>
    <?= $form->field($contractExpirationDate, 'month')->textInput() ?>
    <?= $form->field($contractExpirationDate, 'day')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

It is log result for: Yii::info(Yii::$app->request->post(),'test') in debugger as you seen only last DatePart available but I have two DatePart model instance (contractDate and contractExpirationDate):

[
    '_csrf' => 'Vl81R0ZvMk1hD1oELT9aDzkIe3EPHFgiIBJTBhA9RD8GbFM.AhlVBw==',
    'Person' => [
        'name' => 'test name',
        'family' => 'test family',
        'mobile' => '09121212123',
    ],
    'DatePart' => [
        'year' => '2015',
        'month' => 'Jun',
        'day' => 'Mon',
    ],
]
like image 492
b24 Avatar asked Jul 19 '14 06:07

b24


1 Answers

Controller:

    $model = new Person(['scenario' => 'create_update']);
    $dates = [
        'contractDate' => new DatePart(),
        'contractExpirationDate' => new DatePart()
    ];
    if ($model->load(Yii::$app->request->post())) {

        if (Model::loadMultiple($dates, Yii::$app->request->post()) && Model::validateMultiple($dates)) {
            foreach ($dates as $date) {
                $date->save();
            }
            // or
            $contractDate = $dates['contractDate'];
            $contractExpirationDate = $dates['contractExpirationDate'];
            // ...... some logic

            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }
    }
    else {
            return $this->render('create', [
                'model' => $model,
                'dates' => $dates
            ]);
    }

View Form:

<?= $form->field($dates, '[contractDate]year')->textInput() ?>
<?= $form->field($dates, '[contractDate]month')->textInput() ?>
<?= $form->field($dates, '[contractDate]day')->textInput() ?>

<?= $form->field($dates, '[contractExpirationDate]year')->textInput() ?>
<?= $form->field($dates, '[contractExpirationDate]month')->textInput() ?>
<?= $form->field($dates, '[contractExpirationDate]day')->textInput() ?>
like image 132
zelenin Avatar answered Oct 06 '22 00:10

zelenin