Save in two models only one action

Hi to everyone again! I have a new problem, i’m trying save in two models using only one action of controller, the first model (real model of the controller) save perfectly, but the second (this is in a foreach) don’t save, i dont know why, because i see the code is correct.

My actionCreate code:




public function actionCreate()

{

	$model=new Curriculum;

	$catmodel = new CatCurr;

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

	{

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

		if($model->save())

		{

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

			{				

				$prueba=$_POST['CatCurr'];

				foreach($prueba as $id_cat)

				{

                                        //i writed here the redirect and enter here

					$catcurr = new CatCurr;

					$catcurr->id_curr = $model->id;

					$catcurr->id_cat = $id_cat;

					if($catcurr->save())

                                                //it dont enter here

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

				}

			}

		}

	}

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

		'model'=>$model,'catmodel'=>$catmodel

	));

}



My View Code:




<div class="form">


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

	'id'=>'curriculum-form',

	'enableAjaxValidation'=>false,

)); ?>


	<?php echo $form->errorSummary($model); ?>

	<div class="row">

		<?php

		echo CHtml::label('Persona:','Curriculum_id_persona');

        echo $form->dropDownList($model,'id_persona',CHtml::listData(Persona::model()->findAll(),'id','nom'));

        ?>

    </div>




	<div class="row">

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

		<?php echo $form->textField($model,'data_entrada'); ?>

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

	</div>




	<div class="row">

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

		<?php echo $form->textArea($model,'observacions',array('rows'=>6, 'cols'=>50)); ?>

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

	</div>

         <!-------------------------------->

	 <!--THE CODE OF THE OTHER MODEL -->

         <!-------------------------------->

    <div class="row">

    	<table>

    	<?php 

			$categories = CategoriesProfesionals::model()->findAll();

			for($i=0; $i<CategoriesProfesionals::model()->count(); $i+=3)

			{

				echo "<tr>";

						//check1

				//echo "<td>".CHtml::activeCheckBox($catmodel, 'id_cat', array('value'=>'$categories[$i]->id', 'name'=>'CatCurr[id_cat][]'))."</td>";

				echo "<td><input type='checkbox' value='".$categories[$i]->id."' name='CatCurr[]' id='CatCurr_id".$i."' /></td>";

						//label1

				echo "<td><label for='CatCurr_id".($i)."'>".$categories[$i]->nom."</label></td>";

						//check2

				echo "<td><input type='checkbox' value='".$categories[$i+1]->id."' name='CatCurr[]' id='CatCurr_id".($i+1)."' /></td>";

						//label2

				echo "<td><label for='CatCurr_id".($i+1)."'>".$categories[$i+1]->nom."</label></td>";

						//check3

				echo "<td><input type='checkbox' value='".$categories[$i+2]->id."' name='CatCurr[]' id='CatCurr_id".($i+2)."' /></td>";

						//label3

				echo "<td><label for='CatCurr_id".($i+2)."'>".$categories[$i+2]->nom."</label></td>";

				echo "</tr>";

			}

		?>

        </table>

    </div>

	<div class="row buttons">

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

	</div>


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


</div><!-- form -->



Grettings and thanks,

s0mk3

$catcurr->save() validates before saving… so maybe the validation fails…

and what is the point of the foreach() if you have a redirect…

ok, my error… i solved it


$catcurr->save(false);