Yii2 Multiple Model and tabular data issue

I have two tables:

-players

-registrations

Each Registrations record can have many Players associated with it.

I want to be able to fill out the registration/create form and submit one registration record and however many players (up to the four I put on the form).

My registration/create action gives me a syntax error:

syntax error, unexpected ‘$registration’ (T_VARIABLE)

Can someone help me see what I’m doing wrong to make this work? It’s one of the biggest pieces of the puzzle I need to work locally before I can see if it works online.

I created a controller named RegistrationController.php


<?php

 

namespace backend\controllers;

 

use Yii;

use backend\models\Registrations;

use backend\models\Players;

use backend\models\search\RegistrationsSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use yii\helpers\Json;

use yii\helpers\ArrayHelper;

use yii\base\Exception;

 

/**

 * RegistrationController implements the CRUD actions for registration and players models.

 */

 

class RegistrationController extends Controller

{

 

public function actionCreate()

{

        $registration = new Registrations();

        $players = [];

        for ($i = 0; $i < 5; $i++) {

            $players[] = new Players();

        }

    if ($registration->load(Yii::$app->request->post()) {

        $registration->save();

    }

 

    if (Model::loadMultiple($players, Yii::$app->request->post()) {

        foreach ($players as $player) {

            $player->regsitrationID = $registration->ID;

            $player->save();

        }

    }

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

            'registration'=>$registration,

            'players' => $players,

            ]);

    }

}

My create.php is the standard and rendersPartial _form.php (which is below):


<?php

 

use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\web\View;

use backend\models\registration;

use backend\models\players;

use backend\models\registrations;

 

/* @var $this yii\web\View */

/* @var $model backend\models\registration */

/* @var $form yii\widgets\ActiveForm */

?>

 

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(

    'id'=>'user-form',

    'enableAjaxValidation'=>false,

)); ?>

 

    <p class="note">Fields with <span class="required">*</span> are required.</p>

 

    <div class="row">

 

    <?= $form->field($registration, 'transactionID')->hiddenInput(['value'=>'2'])->label(false) ?>

 

    <?= $form->field($registration, 'divisionLevelID')->dropDownList($model->DivisionLevelAssignmentList, [ 'prompt' => 'Please Choose One' ]);?>

 

    <?= $form->field($registration, 'field')->dropDownList($model->FieldsList, [ 'prompt' => 'Please Choose One' ]);?>

 

    <?= $form->field($registration, 'net')->textInput() ?>

 

    <?= $form->field($registration, 'team')->textInput() ?>

 

    <?= $form->field($registration, 'notes')->textarea(['rows' => 6]) ?>

 

    </div>

 

    //////////////////////////////////////////////////////

    <h2>Player 1 Info</h2> 

 

    <div class="row">

    <?= $form->field($players, '[]first')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]last')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]phone')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]email')->textInput(['maxlength' => true]) ?>

    </div>

 

    //////////////////////////////////////////////////////

    <h2>Player 2 Info</h2> 

 

    <div class="row">

    <?= $form->field($players, '[]first')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]last')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]phone')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]email')->textInput(['maxlength' => true]) ?>

    </div>

 

    //////////////////////////////////////////////////////

    <h2>Player 3 Info</h2> 

 

    <div class="row">

    <?= $form->field($players, '[]first')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]last')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]phone')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]email')->textInput(['maxlength' => true]) ?>

    </div>

 

    //////////////////////////////////////////////////////

 

    //////////////////////////////////////////////////////

    <h2>Player 4 Info</h2> 

 

    <div class="row">

    <?= $form->field($players, '[]first')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]last')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]phone')->textInput(['maxlength' => true]) ?>

 

    <?= $form->field($players, '[]email')->textInput(['maxlength' => true]) ?>

    </div>

 

    //////////////////////////////////////////////////////

 

<?php $this->endWidget(); ?>

 

</div><!-- form -->

In which line of code does the error occur? The error message should tell you that.

Line 31 $registration->save();

The brackets in your code don’t match, several are missing.

Which IDE do you use? If you are using a mere text editor, have a look at NetBeans or PhpStorm.

This code should work:




    public function actionCreate()

    {

        $registration = new Registrations();

        $players = [];

        for ($i = 0; $i < 5; $i++) {

            $players[] = new Players();

        }

        if ($registration->load(Yii::$app->request->post())) {

            $registration->save();

        }


        if (Model::loadMultiple($players, Yii::$app->request->post())) {

            foreach ($players as $player) {

                $player->regsitrationID = $registration->ID;

                $player->save();

            }

        }

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

            'registration'=>$registration,

            'players' => $players,

        ]);

    }