Olga
Olga

Reputation: 443

Getting data for 2 different models - Yii2

I have a form that inside looks like that:

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

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

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

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

Then I collect received data with controller, like that:

$model = new PresentationPlaceHistory();
$presentation = new Presentations();

if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {
    $pres = $presentation->findOne($model->presentation_id);
    $pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
    $pres->couples = $presentation->load(Yii::$app->request->post('couples'));
    $pres->save();
    return $this->redirect(['view', 'id' => $model->id]);
}
else {
    return $this->render('create', [
        'model' => $model,
        'presentation' => $presentation,
    ]);
}

But in the effect, it saves all, beside 'attendance' and 'couples', which are set to 0 (before this form they were null).

It doesn't matter what number I put, I get zero. It doesn't even have to be number, because validation doesn't work at all.

Upvotes: 0

Views: 274

Answers (3)

Satisfier
Satisfier

Reputation: 95

Kinda messy code u got here. First of all, I suggest you not to use load() method, but its my personal preference. Read more about load. This method returns boolean, that is why you are getting 0 in model properties. In general, your code should look more like:

$model->load(Yii::$app->request->post());
if ($model->save()) {
    $pres = $presentation->findOne($model->presentation_id);

    $pres->attendance = $presentation->attendance;
    //etc ...
    $pres->save()
}

I dont know what is the point of your code, but this looks kinda pointless. Try to work with attributes, it is an array of all model properties. Or assign manually wanted properties of the model.

Upvotes: 1

Naisa purushotham
Naisa purushotham

Reputation: 913

problem with your controller code, change following two lines

if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {

        $post_data=  Yii::$app->request->post();

        $pres = $presentation->findOne($model->presentation_id);

        /* change these two lines */
        $pres->attendance = $post_data['Presentations']['attendance'];
        $pres->couples =$post_data['Presentations']['couples'];
        /* change these two lines */

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

Upvotes: 0

leninhasda
leninhasda

Reputation: 1730

Change following two lines:

$pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
$pres->couples = $presentation->load(Yii::$app->request->post('couples'));

// to this
$pres->attendance = $presentation->attendance;
$pres->couples = $presentation->couples;

You have already loaded $presentation values here $presentation->load(Yii::$app->request->post()) and should be available to access directly.

Upvotes: 1

Related Questions