title: Import csv to mysql using Yii framework
body: <p>I have a problem with uploading csv file to database. This is my view:</p>
  
  <pre><code>&lt;?php
  $this-&gt;breadcrumbs = array(
  __('People') =&gt; array('/contacts'),
  __('Persons') =&gt; array('admin'),
  __('Manage'),
  );?&gt;
  
  
  
  &lt;h1&gt;&lt;?php echo __('People'); ?&gt; &lt;small&gt;&lt;?php echo __('import contacts'); ?&gt;&lt;/small&gt;&lt;/h1&gt;&lt;br/&gt;
  &lt;div class="form"&gt;
  
  &lt;?php
  $form = $this-&gt;beginWidget('bootstrap.widgets.TbActiveForm', array(
  'id'=&gt;'service-form',
  'enableAjaxValidation'=&gt;false,
  'method'=&gt;'post',
  'type'=&gt;'horizontal',
  'htmlOptions'=&gt;array(
      'enctype'=&gt;'multipart/form-data'
  )
  )); ?&gt;
  
  &lt;fieldset&gt;
  
  
      &lt;?php echo $form-&gt;errorSummary($model, 'Opps!!!', null, array('class'=&gt;'alert alert-error span12')); ?&gt;
  
      &lt;div class="control-group"&gt;     
          &lt;div class="span4"&gt;
                              &lt;div class="control-group &lt;?php if ($model-&gt;hasErrors('postcode')) echo "error"; ?&gt;"&gt;
      &lt;?php echo $form-&gt;labelEx($model,'file'); ?&gt;
      &lt;?php echo $form-&gt;fileField($model,'file'); ?&gt;
      &lt;?php echo $form-&gt;error($model,'file'); ?&gt;
                          &lt;/div&gt;
  
  
          &lt;/div&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', 'icon'=&gt;'ok white', 'label'=&gt;'UPLOAD')); ?&gt;
          &lt;?php $this-&gt;widget('bootstrap.widgets.TbButton', array('buttonType'=&gt;'reset', 'icon'=&gt;'remove', 'label'=&gt;'Reset')); ?&gt;
      &lt;/div&gt;
  
  &lt;/fieldset&gt;
  
  &lt;?php $this-&gt;endWidget(); ?&gt;
  
  &lt;/div&gt;&lt;!-- form --&gt;
  </code></pre>
  
  <p>This is my model:</p>
  
  <pre><code>&lt;?php
  class UserImportForm extends CFormModel
  {
  public $file;
  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
      // NOTE: you should only define rules for those attributes that
      // will receive user inputs.
      return array(  
           array('file', 'file', 
                                          'types'=&gt;'csv',
                                          'maxSize'=&gt;1024 * 1024 * 10, // 10MB
                                          'tooLarge'=&gt;'The file was larger than 10MB. Please upload a smaller file.',
                                          'allowEmpty' =&gt; false
                            ),
                 );
  }
  
  /**
   * @return array customized attribute labels (name=&gt;label)
   */
  public function attributeLabels()
  {
      return array(
          'file' =&gt; 'Select file',
      );
   }
  
  }
  ?&gt;
  </code></pre>
  
  <p>And this is my function in Controller</p>
  
  <pre><code>public function actionImportCSV()
      {
         $model=new UserImportForm;
  
         if(isset($_POST['UserImportForm']))
           {
  
             $model-&gt;attributes=$_POST['UserImportForm'];
  
             if($model-&gt;validate())
               {
  
                $csvFile=CUploadedFile::getInstance($model,'file');  
                $tempLoc=$csvFile-&gt;getTempName();
  
                  $sql="LOAD DATA LOCAL INFILE '".$tempLoc."'
      INTO TABLE `contacts`
      ";
                  $connection=Yii::app()-&gt;db;
                  $transaction=$connection-&gt;beginTransaction();
                      try
                          {
  
                              $connection-&gt;createCommand($sql)-&gt;execute();
                              $transaction-&gt;commit();
                          }
                          catch(Exception $e) // an exception is raised if a query fails
                           {
                              echo "&lt;pre&gt;";
                              print_r($e);
                              echo "&lt;/pre&gt;";
                              exit;
                              $transaction-&gt;rollBack();
  
                           }
                    $this-&gt;redirect(Yii::app()-&gt;createUrl("/contacts/importCSV"));
  
  
               }
           }  
  
         $this-&gt;render("importcsv",array('model'=&gt;$model));
       }
  </code></pre>
  
  <p>And I have a problem with LOAD DATA, cause mySQL version don't support LOAD DATA. Anyone knows how can I replace load data to import csv file?</p>