Update 2 models in one controller

I am having a form where i want to update two different models (Photos & PhotosTags) when there is a record in the database everything is working fine but when it is empty i am getting an error 404 which is normal, how can i tell it to ignore that so i can update/insert the empty record?

PhotosController




	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);

		$modelTags=$this->loadTagsModel($id);

		

		$this->layout='column3';

		$this->picid=$model->id;

				

		list($this->imgWidth, $this->imgHeight) = getimagesize('images/originals/'.$model->id.'.jpg');

		

		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Photos'], $_POST['PhotosTags']))

		{

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

			$modelTags->attributes=$_POST['PhotosTags'];

			 

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

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

		}

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

				'model'=>$model,

				'modelTags'=>$modelTags,

			));


	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Photos::model()->with('photo_id')->findByPk((int)$id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	public function loadTagsModel($id)

	{

		$modelTags=PhotosTags::model()->findByPk((int)$id);		

		if($modelTags===null)	

			throw new CHttpException(404,'The requested page does not exist.');

		return $modelTags;

	}



_form.php




<?php if($model->status == 0 || $model->status == 2){ ?>


    <div class="form">


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

        'id'=>'update-form',

        'enableAjaxValidation'=>false,

    )); ?>

        

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

    

        <div class="row">

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

            <?php echo $form->textField($model,'title',array('size'=>40,'maxlength'=>255)); ?>

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

        </div>

    

        <div class="row">

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

            <?php echo $form->textField($model,'equipment',array('size'=>40,'maxlength'=>255)); ?>

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

        </div>

        

         <div class="row">

            <?php echo $form->labelEx($modelTags,'tags'); ?>

            <?php echo $form->textField($modelTags,'tags',array('size'=>40,'maxlength'=>255)); ?>

            <?php echo $form->error($modelTags,'tags'); ?>       

        </div>       

             

        <div class="row buttons">

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

        </div>

    

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

    

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


<?php } ?>



Your question is not clear… who is empty? the record you are editing… or the database…

If the database is empty… why would you update it?

Sorry you are right, when there is a record in the PhotosTags model i am able to update it, but my problem is when there is no record what is then the best way to insert that new record into the database?

One solution would be to not fire the exception in loadTagsModel() if $modeltags is null

something like:




if($modelTags==null)

   $modelTags=new ModelTags;



The other solution would be to always be sure to have a corresponding record in ModelTags… so that when you insert a record in Photos you insert a record in PhotoTags, too even if the tags are empty…

Ok thanks that worked which of the two is the best way to go?

There is no best way… both works… it’s up to you, your preferences and your needs (different reports, joins, etc.)…

If you choose the first version… than before saving you should check if there is any tag to be saved… so that if no tags are entered you don’t save anything (to be consistent with the create method)… and every time you get the model you need to check if there are tags for it…

the second one is a bit easy as you don’t need to check every time if tags is NULL… (you take care for that on the createAction)… but this way the database can have many empty records…

Ok thanks for your answer will go for the first method then to avoid empty records.

mmmm how do you setup the dataprovider to search in two different models?

Someone any idea to help me on the road here?