title: Fatal error: Call to a member function saveAs() on a non-object on FileField Update
body: <p>I'm working with a FileField on Yii. On create it's working fine, but I'm having troubles on update. </p>
  
  <p>Let's say that someone save a form name city and an image on filefield but forget to put the type, so UPDATE the form, edit the type and save it again, here is where the problem comes. At the moment of save the update, the filefield delete the image that have uploaded before. I find some codes, but now instead of saving and deleting the image, is sending me this error:</p>
  
  <p>Fatal error: Call to a member function saveAs() on a non-object.</p>
  
  <p>Here is my form:</p>
  
  <pre><code>&lt;?php 
  Yii::app()-&gt;user-&gt;setFlash('success', '&lt;strong&gt;Well done!&lt;/strong&gt; You successfully read this important alert message.');
  $this-&gt;widget('bootstrap.widgets.TbAlert', array(
      'block'=&gt;true, // display a larger alert block?
      'fade'=&gt;true, // use transitions?
      'closeText'=&gt;'×', // close link text - if set to false, no close link is displayed
      'alerts'=&gt;array( // configurations per alert type
          'error'=&gt;array('block'=&gt;true, 'fade'=&gt;true, 'closeText'=&gt;'×'), // success, info, warning, error or danger
      ),
  ));
  ?&gt;
  
  &lt;div class="form"&gt;
  
          &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array(
          'id'=&gt;'reportes-form',
          'enableAjaxValidation'=&gt;false,
          'htmlOptions' =&gt; array('enctype' =&gt; 'multipart/form-data'),
          )); ?&gt;
          &lt;?php echo $form-&gt;errorSummary($model); ?&gt;
          &lt;p class="help-block"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model, 'name'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'name'); ?&gt;
          &lt;?php echo $form-&gt;error($model, 'name'); ?&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model, 'city'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'city'); ?&gt;
          &lt;?php echo $form-&gt;error($model, 'city'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'id_type'); ?&gt;
          &lt;?php $datos = CHtml::listData(Types::model()-&gt;findAll(array('condition'=&gt;'', 'order'=&gt;'')), 'id', 'name');
              echo CHtml::activeDropDownList($model,'id_type',$datos,array('prompt'=&gt;'Select', 'disabled'=&gt; '')); 
          ?&gt;
      &lt;/div&gt;
      &lt;div class="row" &gt;
          &lt;?php echo $form-&gt;labelEx($model, 'description'); ?&gt;
          &lt;?php echo $form-&gt;textArea($model,'description'); ?&gt;
          &lt;?php echo $form-&gt;error($model, 'description'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
  
          &lt;?php echo $form-&gt;labelEx($model, 'image'); ?&gt;
          &lt;?php if ($model-&gt;image): ?&gt;
              &lt;div&gt;Existing image: &lt;?php echo CHtml::encode($model-&gt;image); ?&gt;&lt;/div&gt;
              &lt;div&gt;
                  &lt;img src="&lt;?php echo Yii::app()-&gt;request-&gt;baseUrl.'/images/uploads/'.$model-&gt;image?&gt;" width="180" height="180" /&gt;
              &lt;/div&gt;
          &lt;?php endif; ?&gt;
  
          &lt;?php  echo $form-&gt;fileField($model, 'image'); ?&gt;
          &lt;?php echo $form-&gt;error($model, 'image'); ?&gt;
      &lt;/div&gt;
  
  &lt;div class="form-actions"&gt;
      &lt;?php $this-&gt;widget('bootstrap.widgets.TbButton', array(
              'buttonType'=&gt;'submit',
              'type'=&gt;'primary',
              'label'=&gt;$model-&gt;isNewRecord ? 'Create' : 'Save',
          )); ?&gt;
  &lt;/div&gt;
  
  &lt;?php $this-&gt;endWidget(); ?&gt;
  </code></pre>
  
  <p>Here is my controller update:</p>
  
  <pre><code>public function actionUpdate($id)
      {   
          $model=$this-&gt;loadModel($id);
  
  
          // Uncomment the following line if AJAX validation is needed
          // $this-&gt;performAjaxValidation($model);
  
          if(isset($_POST['Restaurants']))
          {
              $model-&gt;attributes=$_POST['Restaurants'];
              $oldFileName    = $_POST['Restaurants']['image'];
              $model-&gt;image= CUploadedFile::getInstanceByName('Restaurants[image]');
              $path = Yii::getPathOfAlias('webroot')."/images/uploads/";
  
  
  
  
              if($model-&gt;save()) {
                  if($model-&gt;image == null){
  
                      $model-&gt;image-&gt;saveAs(Yii::getPathOfAlias('webroot')."/images/uploads/".$oldFileName);
                      $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));
                  }
                  else
                  {
                      $model-&gt;image-&gt;saveAs(Yii::getPathOfAlias('webroot')."/images/uploads/".$model-&gt;image);
                      chmod( $path.$model-&gt;image, 0777 );
                      $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));
  
  
                  }
  
              }
          }
  
          $this-&gt;render('update',array(
          'model'=&gt;$model,
          ));
      }
  </code></pre>
  
  <p>Making a <code>var_dump</code> of <code>$model-&gt;file</code> sends me <code>null</code>, and <code>$oldFileName</code> have the name of the image uploaded on the first time. So I'm guessing this should save on <code>$model-&gt;image</code> the old name without trouble. What I'm missing?</p>