Thursday, January 3, 2013

Save Uploaded/Parsed File to Database Table

Hello,
       I am having some trouble saving an uploaded file to a table in MySQL database.
With that said, I have 2 fields in this table that references 2 other tables. One being user_id (which lets you know which user uploaded the file) , and the coverage_id(which let s you know which coverage the uploaded file belongs too).

 I have tried adding this to my model with no luck.... $this->Spreadsheet->saveAll($pulledData); 

Also I have added this to my model as well..... 

 public $hasOne = array(
                      'Spreadsheet' => array(
                  'className' => 'Spreadsheet',
                  'foreignKey' => 'coverage_id',
                  'dependent' => false,
                  'conditions' => '',
                  'fields' => '',
                  'order' => '',
                  'limit' => '',
                  'offset' => '',
                  'exclusive' => '',
                  'finderQuery' => '',
                  'counterQuery' => ''
            );

Also I created a model called Spreadsheet and it is as follows....


      public $name = 'Spreadsheet';
      public $hasone = 'Coverage';
      
      
      public $belongsTo = array(
            'Coverage' => array(
                  'className' => 'Coverage',
                  'foreignKey' => 'coverage_id'
            ),
            'User' => array(
                  'className' => 'User',
                  'foreignKey' => 'user_id',
                  
            )
      );
      
};

MySQL database table is as follows 

id(varchar)
filename(varchar)
file(mediumblob)
user_id(varchar)
coverage_id(varchar)
created(datetime)
modified(datetime)

This is my model I use to process and save data from the uploaded file:

public function saveSpreadsheet($uploadedFile) {
            if(!empty($uploadedFile) && !empty($uploadedFile['tmp_name'])) {
                  if($uploadedFile['error'] == UPLOAD_ERR_OK) {
                        $pulledData = $this->extract($uploadedFile['tmp_name']);

                        if(!empty($pulledData)){
                              $pulledData = $this->Declination->ContactType->setSpreadsheetContactTypeIds($pulledData);
                              if(!empty($pulledData[$this->alias])) {
                                    $this->save($pulledData[$this->alias]);
                                    unset($pulledData[$this->alias]);
                                    $coverageId = $this->id;
                                    $pulledData = $this->setSpreadsheetCoverageIds($pulledData, $coverageId);
                                    $pulledData = $this->CoverageInsured->setSpreadsheetCoverageInsuredIds($pulledData);
                                    //$pulledData = $this->Policy->Location->setupSpreadsheetPolicyLocation($pulledData, $coverageId);
                              }
                              $this->CoverageInsured->save($pulledData['CoverageInsured']);
                              $this->Declination->saveAll($pulledData['Declination']);
                              $this->savePolicyData($pulledData);
                              $this->Spreadsheet->saveAll($pulledData);
                              //$this->Policy->Location->saveAll($pulledData['Location']);
                              //$this->Policy->Company->saveAll($pulledData['Policy']);
                              return $coverageId;
                        }
                  }
            }
            exit();
            return false;
      }

Here is my current controller:

public function processSpreadsheet() {
            if(!empty($this->data)) {
                  if(isset($this->data['Coverage']) && !empty($this->data['Coverage'])) {
                        if(array_key_exists('Spreadsheet', $this->data['Coverage']) && $this->data['Coverage']['Spreadsheet']['size'] > 0) {
                              $uploadedFile = $this->data['Coverage']['Spreadsheet'];
                              $spreadsheetId = $this->Coverage->saveSpreadsheet($uploadedFile);
                              if($spreadsheetId) {
                                    $this->Session->setFlash(__('Spreadsheet processed successfully.', true));
                                    $this->redirect(array(
                                          'action' => 'edit',
                                          $spreadsheetId
                                    ));
                              } else {
                                    $this->Session->setFlash(__('Spreadsheet processing failed.', true));
                                    $this->redirect(array(
                                          'action' => 'add'
                                    ));
                              }
                        }
                  }
            }
      }
      
Finally here is my view:

<?php echo $this->UiForm->create(null, array(
                        'type' => 'file',
                        'action' => 'processSpreadsheet',
                        'enctype' => 'multipart/form-data',
                  )); ?>            
                        <?php echo $this->UiForm->input('Spreadsheet', array(                              
                              'label' => '',
                              
                              'rel'=>'tooltip',
                              'title' => 'Choose a completed SLIC Policy Template from your local system to import.',
                              'data-trigger'=>'hover',
                              'type' => 'file',
                        )); ?>
                  <?php echo $this->UiForm->end(array('label' =>'Upload' ,'class'=>'btn', 'id'=>'upload-doc' )); ?>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: