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 } ?>