Can't Save - Many Multi Model Forms

I have a bunch of models that are related in the following way.

(View Attached Photo it would not let me imbed it because this is my first post)

2549

Screen shot 2012-02-14 at 9.06.28 AM.png

My main model with the controller is Recipe_Category.

in actionCreate() I have the following:


$model=new RecipeCategory;

		$recipes=new Recipes;

		$recipeImages=new RecipeImages;

		$recipeSteps=new RecipeSteps;

		$recipeIngredients=new RecipeIngredients;




		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			$recipes->attributes=$_POST['Recipes'];

			$recipeSteps->attributes=$_POST['RecipeSteps'];

			$recipeImages->attributes=$_POST['RecipeImages'];

		//	$recipeIngredients->attributes=$_POST['RecipeIngredients'];




			if($model->save())

			$recipes->recipe_category = $model->recipe_category;

			$recipeSteps->recipe_id = $recipes->recipe_id;

			$recipeImages->recipe_id = $recipes->recipe_id;

			//$recipeIngredients->recipe_id = $recipes->recipe_id;


			$recipes->save();

			$recipeSteps->save();

			$recipeImages->save();

			//$recipeIngredients->save();

			

			

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

		}


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

			'model'=>$model,

			'recipes'=>$recipes,

			'recipeSteps'=>$recipeSteps,

			'recipeImages'=>$recipeImages,

			'recipeIngredients'=>$recipeIngredients

		));

However, only Recipe_Category and Recipes will save to the database and when I display the results in the View Recipes shows "Not Set"

I’m finding Yii to be more confusing than If I did all of this with standard PHP/MySQL :(

$recipes->recipe_id might be NULL when you try to assign it to $recipeSteps->recipe_id and $recipeImages->recipe_id.

Assuming that $recipes->recipe_id is an auto incremental PK, you have to save $recipes before you assign the recipe_id to others.

Duh, Now I feel stupid that makes sense.

Hello,

I had to deal with something like that, it may help, I am not sure its the best way to do! I am learning programming in the same time as Yii ;D

I’ve used an extension made by Alexander Kochetov I am looking for the way to make it save my models* , till now I can only validate all models in the same form. :blink:<_<


  • The doc explain how to do it, but my English don’t really understand everything, i have to translate part by part :rolleyes:

what i did to save, is pass false to the save method for each model, after validating everything of corse,

and in the process of saving under a transaction, I affect each insertedID to the FK


 

              	$db=Yii::app()->db;


              	$extTransFlag=$db->getCurrentTransaction();


              	if($extTransFlag===null) $transaction=$db->beginTransaction();

              	try

              	{

                    	$modelUser->save(false);

                    	// saving process with affectation of ids to the Foreign Keys

         				//  ......


   			if($extTransFlag===null) $transaction->commit();


          	catch(Exception $e)

              	{

                  	if($extTransFlag===null) $transaction->rollBack();

              	}

like that you should be sure to have everything on the database or nothing ;D I hope ;D