Jump to content
Larry Ullman's Book Forums

Yii2 Multiple Models And Tabular Input Combined


melaniecarr23
 Share

Recommended Posts

I have two tables:

-players

-registrations

 

Each Registrations record can have many Players associated with it.  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 -->
Link to comment
Share on other sites

Hi,

 

The error message you are describing is a normal PHP error, not something specific to Yii.  You haven't given us the actual line number of where the error has occurred, and I can't see anything that is immediately obvious (except CActiveForm).

 

The unexpected message usually means that the prior statement is incomplete.  I.e. missing semi-colons, braces etc.  I would be looking for that.  Here is an example (notice the missing semi-colon after 'brent').

 

<?php


$name = 'brent'
$registrations = true;
This is the output when I run it:
 
Parse error: syntax error, unexpected '$registrations' (T_VARIABLE) in C:\programming\error1.php on line 4
Also not sure if it is related, it appears you are mixing Yii1 and Yii2.  CActiveForm is part of Yii1.  This could be where you are experiencing a problem.
 
regards,
 
Brent
Link to comment
Share on other sites

 Share

×
×
  • Create New...