Yii Framework Forum: Yii2 Multiple Model and tabular data issue - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii2 Multiple Model and tabular data issue getting errors in my code

#1 User is offline   mcadio 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 12-January 16

Posted 23 May 2016 - 07:23 PM

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 -->

0

#2 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 321
  • Joined: 14-July 12
  • Location:Berlin

Posted 23 May 2016 - 10:51 PM

In which line of code does the error occur? The error message should tell you that.
Life begins outside the comfort zone.
0

#3 User is offline   mcadio 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 12-January 16

Posted 24 May 2016 - 07:32 AM

View PostPatrick Jones, on 23 May 2016 - 10:51 PM, said:

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


Line 31 $registration->save();
0

#4 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 321
  • Joined: 14-July 12
  • Location:Berlin

Posted 26 May 2016 - 07:40 AM

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,
        ]);
    }

Life begins outside the comfort zone.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users