actionCreate with two models

Hi,

In advance, sorry for my English.

I work with the framework yii 1.1.7, MySQL database and xampp.

I want to create a user using his name and the project which it is attached (the project name is already created).

Many topics already speaking about that, but I haven’t found an answer to my problem.

Here are some of my database and my code.

database :


TABLE `user` ( `id` , 

`rpi` varchar(7) NOT NULL}

  

TABLE `userprojet` ( `id` ,

  `userId` NOT NULL COMMENT 'constraint foreign key (userId) references user(id)',

  `projetName` varchar(12) NOT NULL COMMENT 'constraint foreign key (projetName) references projet(name)'}

  

TABLE `projetvehicule` ( `id` , 

`name` varchar(12) NOT NULL }



UserController :


	public function actionCreate() {

				

	    $model = new User;

	    $modelProjet = new Userprojet;

		

	    if(isset($_POST['User'], $_POST['Userprojet']))

	    {

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

	        $modelProjet->attributes=$_POST['Userprojet'];

	 

	        //validate BOTH $model and $model

	        $valid = $model->validate();

	        $valid = $modelProjet->validate() && $valid;

	        	 

	        if($valid)

	        { 

	            if($model->save(false))  

			    {  

			        $modelProjet->userId = $model->id;  

			        $modelProjet->save(false);  

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

			    }

	        }

	    }

	    

	

	    $this->render('create', array(

	        'user'=>$model,

	        'userprojet'=>$modelProjet,

	    	));

	  

	}

user/_form :


	<?php //echo $form->errorSummary(array($model, $modelProjet)); ?>


		<div class="row">

		<?php echo $form->labelEx($model,'rpi'); ?>

		<?php echo $form->textField($model, 'rpi', array('maxlength' => 7)); ?>

		<?php echo $form->error($model,'rpi'); ?>

		</div><!-- row -->


		<div class="row">

		<?php echo $form->labelEx($modelProjet, 'projetName'); ?>

		<?php echo $form->dropDownList($modelProjet, 'projetName', GxHtml::listDataEx(Userprojet::model()->findAllAttributes(null, true))); ?>

		<?php echo $form->error($modelProjet, 'projetName'); ?>

		</div><!-- row -->

user/create :


$this->renderPartial('_form', array(

		'user' => $model,

		'userprojet'=>$modelProjet,

		'buttons' => 'create'));

And in response, I have this error :


Create User


Fields with * are required. 

Fatal error: Call to a member function [i]isAttributeRequired[/i]() on a non-object in C:\xampp\htdocs\www\framework\web\helpers\CHtml.php on line 1166



Nothing more :huh:

Why is it specified that attributes are required?

I know that all the requested attributes are required.

Regards

Hi Fog, welcome to the forum.

In your code, ‘user’($model) and ‘userprojet’($modelProjet) are not passed to the view and the form as you intended.

UserController :


	public function actionCreate() {

				

	    $model = new User;

	    $modelProjet = new Userprojet;

		

	    ....

	    $this->render('create', array(

-	        'user'=>$model,

-	        'userprojet'=>$modelProjet,

+	        'model'=>$model,

+	        'modelProjet'=>$modelProjet,

	    	));

	  

	}



user/create :


$this->renderPartial('_form', array(

-		'user' => $model,

-		'userprojet'=>$modelProjet,

+		'model' => $model,

+		'modelProjet'=>$modelProjet,

		'buttons' => 'create'));



Thanks you for your help softark.

I didn’t understand that objects in renderPartial must have the same name.

Well, the objects in views and forms can have any names.

But you have to pass the objects to them with the names that are used in them.

For example:




// UserController

            $this->render('create', array(

                'xxx'=>$model, // $xxx in create view is $model

                'yyy'=>$modelProjet, // $yyy in create view is $modelProjet

                ));


// user/create

$this->renderPartial('_form', array(

                'foo'=>$xxx, // $foo in _form is $xxx

                'bar'=>$yyy, // $bar in _form is $yyy

                'buttons' => 'create'));


// _form

                <div class="row">

                <?php echo $form->labelEx($foo,'rpi'); ?>

                <?php echo $form->textField($foo, 'rpi', array('maxlength' => 7)); ?>

                <?php echo $form->error($foo,'rpi'); ?>

                </div><!-- row -->


                <div class="row">

                <?php echo $form->labelEx($bar, 'projetName'); ?>

                <?php echo $form->dropDownList($bar, 'projetName', GxHtml::listDataEx(Userprojet::model()->findAllAttributes(null, true))); ?>

                <?php echo $form->error($bar, 'projetName'); ?>

                </div><!-- row -->