title: Refactoring Controller to Model in Code Igniter
body: <p>It's come to my attention that my image processing code that I currently have in my controller would be better suited in a model, but I'm not sure even where to start to do this.</p>
  
  <p>I have a controller that handles uploading an image, renaming the file and storing it in the database using Doctrine:</p>
  
  <pre><code>&lt;?php
  class Addimage extends Controller 
  {
  
      function index()
      {   
  
  
          $vars['content_view'] = 'uploadimage';
          $this-&gt;load-&gt;view('template', $vars);           
  
      }
  
      public function do_upload()
      {
          $this-&gt;load-&gt;library('form_validation');
          if($this-&gt;_submit_validate() == FALSE)
          {
  /*THIS CODE BLOCK IS DUPLICATED FROM MY HOME PAGE CONTROLLER - this is one of the reasons I want to refactor.*/
              $vars['recentimages'] = Doctrine_Query::create()
          -&gt;select('photo_path')
          -&gt;from('Gif g')
          -&gt;orderBy('g.created_at DESC')
          -&gt;limit(12)
          -&gt;execute();
  
  
  
          $vars['title'] = 'Home';
          $vars['content_view'] = 'welcome_message';
  
  
          $this-&gt;load-&gt;view('template_front', $vars);
  
          }
          else
          {           
  
              $basedir = $this-&gt;config-&gt;item('server_root') . $this-&gt;config-&gt;item('upload_dir');
  
              //If the directory doesn't already exist, create it.
              if (!is_dir($basedir))
              {
                  mkdir($basedir, 0777);
              }           
  
  
  
              $config = array(
                  'allowed_types' =&gt; "gif",           
                  'upload_path' =&gt; $basedir,
                  'remove_spaces' =&gt; true
              );
              $this-&gt;load-&gt;library('upload', $config);
              if(!$this-&gt;upload-&gt;do_upload())
              {
                  $data['error'] = 'There was a problem with the upload';
              }
              else
              {
  
                  $image_data = $this-&gt;upload-&gt;data();                
                  $fileName = $image_data['file_name'];
                  $title = $this-&gt;input-&gt;post('title');
  
                  //Rename File based on how many of that letter
                  //are already in the database
                  $imageCount = Doctrine_Query::create()
                      -&gt;select('COUNT(i.id) as num_images')
                      -&gt;from('Gif i')                 
                      -&gt;execute();
  
                  $imageCount = $imageCount[0]-&gt;num_images++;
                  //Rename file based on title and number of images in db. 
                  $newFileName = preg_replace('/[^a-zA-Z0-9\s]/', '', $title) . '_' . $imageCount . $image_data['file_ext'];
                  rename($basedir . $fileName, $basedir . $newFileName);
  
  
                  $gif = new Gif();
                  $gif-&gt;photo_path = $newFileName;
                  $gif-&gt;title = $title;
                  if(Current_User::user())
                  {
                      $gif-&gt;User = Current_User::user();              
                  }
                  else
                  {                   
                      $gif-&gt;User =  Doctrine::getTable('User')-&gt;findOneById($this-&gt;config-&gt;item('anonuid'));
                  }
                  $gif-&gt;save();
              }
  
  
  
              redirect('/', 'location');
          }
      }
  
      private function _submit_validate()
      {
          $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required');
          return $this-&gt;form_validation-&gt;run();
      }
  
  }
  </code></pre>
  
  <p>I would like to be able to have most of this in a model, because I'm using a template system for the views where my uploadimage.php view is just the upload form so that it can be dropped on any page. Also, I only have experience using Doctrine models.</p>
  
  <p>Thanks for any help in advance</p>