M
M
Maxim2018-06-15 23:47:45
Yii
Maxim, 2018-06-15 23:47:45

How to keep two models and tabular input + Tabular Input?

Hello! Probably already tired of my stupid questions, but I ask for help: D So far, I have not mastered tabular input very well. Especially in conjunction with another model.
I have two tables:
reg_event (Registration for an event)
//Here is the registration data for the event (registration date, where, for which event, and so on)
user (users of the system)
//Here is user data
reg_event_user
//here are two fields
-reg_event_id (registration id from reg_event table)
-user_id
Each registration can have from 1 to 25 participants. Therefore, we have an additional table reg_event_user. We create a new registration, fill in the data, which is written to the reg_event table, and then add 1 to 25 people, which are written to our additional table.
To add new participants I use the TabularInput extension

Controller

У меня получился такой код контроллера:
public function actionCreate()
    {
        $event_id = RegEvent::getEventId();
        $number = RegEvent::setNumber();

        $request = Yii::$app->request;
        $model = new RegEvent(['number'=> $number, 'event_id' => $event_id]);
        $models = [new RegEventUser()];

        if (!isset($model, $models)) {
            throw new NotFoundHttpException("The user was not found.");
        }

        $model->scenario = $model::SCENARIO_FRONTEND;

        if ($request->isPost && $request->post('ajax') !== null) {
            $data = Yii::$app->request->post('RegEventUser', []);
            foreach (array_keys($data) as $index) {
                $models[$index] = new RegEventUser(['reg_event_id' => $model->id]);
            }
            Model::loadMultiple($models, $request->post());
            Yii::$app->response->format = Response::FORMAT_JSON;
            $result = ActiveForm::validateMultiple($models);
            return $result;
        }

        if ($model->load($request->post()) && $model->save() && Model::loadMultiple($models, $request->post())) {
            return $this->redirect(['index']);
        }

        if (Yii::$app->request->isAjax){
            return $this->renderAjax('_form', [
                'model' => $model,
                'models' => $models,
            ]);

        }

        return $this->render('create', [
            'model' => $model,
            'models' => $models,
        ]);
    }

The form
<?php $form = ActiveForm::begin([
        //'enableAjaxValidation'      => true,
        //'enableClientValidation'    => false,
        'validateOnChange'          => false,
        'validateOnSubmit'          => true,
        'validateOnBlur'            => false,
        'options' => ['enctype' => 'multipart/form-data']
    ]); ?>

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


    <?= $form->field($model, 'rang_id')->widget(Select2::classname(), [
        'data' => RegEventRule::getRangs(),
        'options' => ['placeholder' => 'Выберите ранг ...'],
    ]);?>

    <?= $form->field($model, 'discipline_id')->widget(Select2::classname(), [
        'data' => RegEventRule::getDisciplines(),
        'options' => ['placeholder' => 'Выберите дисциплину ...'],
    ]);?>

    <?= $form->field($model, 'nomination_id')->widget(Select2::classname(), [
        'data' => RegEventRule::getNominations(),
        'options' => ['placeholder' => 'Выберите номинацию ...'],
    ]);?>

    <?= $form->field($model, 'category_id')->widget(Select2::classname(), [
        'data' => RegEventRule::getCategories(),
        'options' => ['placeholder' => 'Выберите категорию ...'],
    ]);?>

    <?= TabularInput::widget([
        //'min' => 1,
        'models' => $models,
        'attributeOptions' => [
            //'enableAjaxValidation'      => true,
            //'enableClientValidation'    => false,
            'validateOnChange'          => false,
            'validateOnSubmit'          => true,
            'validateOnBlur'            => false,
        ],
        'columns' => [
            [
                'name'  => 'user_id',
                'type'  => Select2::className(),
                'options' => [
                    'data' => Profile::getDancers(),
                    'options' => ['placeholder' => 'Выберите танцора ...'],
                ]
            ],
        ]
    ]); ?>


The data array comes in this form
5b24251cb76a5572191914.png
Other visualizations to understand
5b24253c954a5729932324.png5b2425577db67254925770.png

Help and tell me what am I doing wrong? And why is the data not stored in an additional table? Apparently, I have not yet indicated saving in the tabular input, but how to connect all this?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-06-16
@myks92

if ($model->load($request->post()) && $model->save() && Model::loadMultiple($models, $request->post())) {
      foreach ($models as $mod) {
          $new_model = new RegEventUser(); // модель связующей таблицы?
          $new_model->reg_event_id = $model->id;
          // заполняете ещё один атрибут
          $new_model->save(false);
      }
      return $this->redirect(['index']);
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question