Using 2 Model In One View

Hello to everybody. I’m new YII Framework. I want to “control” 2 models in one view.

I have 2 models (ideyalar and rey) and one Ideyalar controller in my project.

So i want to do this: when user put some values into textboxes (which these textboxes are related with rey model) and clicked the submit button then the value must insert into the rey table in my database.

I added some lines into my view file:




<?php   

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

       'id' => 'rey-formu',

       'action'=>Yii::app()->createUrl('//ideyalar/qiymet'),

       'enableAjaxValidation' => false

    )); 

?>


  <p id="txt-frame-r">İdeyanın elmi reallığının qiymətləndirilməsi</p>

  <?php echo $form->labelEx(rey::model(), 'elmireal'); ?>

  <?php echo $form->textField(rey::model(), 'elmireal', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error(rey::model(), 'elmireal'); ?>

  <br/><br/>


  <p id="txt-frame-r">Azərbaycanda realiza edilmə ikmanının qiymətləndirilməsi</p>

  <?php echo $form->labelEx(rey::model(), 'azerreal'); ?>

  <?php echo $form->textField(rey::model(), 'azerreal', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error(rey::model(), 'azerreal'); ?>

  <br/><br/>


  <p id="txt-frame-r">Tələb olunan resurslar baxımından rentabelliliyin qiymətləndirilməsi</p>       

  <?php echo $form->labelEx(rey::model(), 'resrent'); ?>

  <?php echo $form->textField(rey::model(), 'resrent', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error(rey::model(), 'resrent'); ?>

  <br/><br/>


  <?php echo CHtml::submitButton(rey::model()->isNewRecord ? 'Qiymet' : 'Save'); ?>


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

In my IdeyalarController.php:


public function actionQiymet ()

{

    $model = new Ideyalar;

        if (isset($_POST['Ideyalar'])) 

        {

            $model->attributes = $_POST['Ideyalar'];

            if ($model->save()) 

            {

                $modelEmail = new rey;

                $modelEmail->attributes = $_POST['rey'];

                $modelEmail->iduser = $model->id;

                if ($modelEmail->save())

                    $this->redirect(['view', 'id' => $model->id]);

            }

        }


    $this->render('viewe', ['model' => $model,]);

}

From 1st look it maybe looks OK but it’s not working properly (the input values aren’t insert to database table). Please help.

Thanks. :)

what happens when you submit the form does send you back to the form or redirects to view page

also do var_dump($_POST[‘rey’]); die; and see if the form data is being sent to the server

it refreshs the page. that’s all. :confused:

And also the values are not insert into the database. where is the problem?

okay I’ll.

then its not saving its probably the validation

I didn’t validate it in controller. maybe this is a problem.

yes that where the problem is, also you should actually create both models in the controller and pass it down to the view




public function actionQiymet()

    {

        $model = new Ideyalar;

        $rey = new Rey; // your second model

        if (isset($_POST['Ideyalar']) && $_POST['Rey']) 

        {

            $model->attributes = $_POST['Ideyalar'];

            $rey->attributes = $_POST['Rey'];

            $rey->iduser = $model->id;


            if ($model->save() && $rey->save()) 

                $this->redirect(['view', 'id' => $model->id]);

        }


        $this->render('viewe', ['model' => $model, 'rey'=>$rey]);

    }

// view


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

   'id' => 'rey-formu',

   'action'=>Yii::app()->createUrl('//ideyalar/qiymet'),

   'enableAjaxValidation' => false

)); ?>

    <?php echo $form->errorSummary([$model, $rey]) ?>


  <p id="txt-frame-r">İdeyanın elmi reallığının qiymətləndirilməsi</p>

  <?php echo $form->labelEx($rey, 'elmireal'); ?>

  <?php echo $form->textField($rey, 'elmireal', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error($rey, 'elmireal'); ?>

  <br/><br/>


  <p id="txt-frame-r">Azərbaycanda realiza edilmə ikmanının qiymətləndirilməsi</p>

  <?php echo $form->labelEx($rey, 'azerreal'); ?>

  <?php echo $form->textField($rey, 'azerreal', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error($rey, 'azerreal'); ?>

  <br/><br/>


  <p id="txt-frame-r">Tələb olunan resurslar baxımından rentabelliliyin qiymətləndirilməsi</p>       

  <?php echo $form->labelEx($rey, 'resrent'); ?>

  <?php echo $form->textField($rey, 'resrent', ['size' => 60, 'maxlength' => 250]); ?>

  <?php echo $form->error($rey, 'resrent'); ?>

  <br/><br/>


  <?php echo CHtml::submitButton($rey->isNewRecord ? 'Qiymet' : 'Save'); ?>


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






sorry but also this is not insert the value to db… :mellow:

I did not test the code. does it display any error when you save the form

no

thanks very much!

but I solved the problem ‘via’ another method. :)