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.....
Also I created a model called Spreadsheet and it is as follows....
MySQL database table is as follows
id(varchar)
filename(varchar)
file(mediumblob)
user_id(varchar)
coverage_id(varchar)
created(datetime)
modified(datetime)
Note: There is a reason my id fields are varchar!
This is my model I use to process and save data from the uploaded file:
Here is my current controller:
Finally here is my view:
Now with that said, these are the errors I get when I excute my script......
Warning (512): SQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails (`ilsa_slic`.`spreadsheets`, CONSTRAINT `spreadsheets_ibfk_1` FOREIGN KEY (`coverage_id`) REFERENCES `coverages` (`id`)) [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: INSERT INTO `spreadsheets` (`modified`, `created`) VALUES ('2013-01-04 09:19:05', '2013-01-04 09:19:05')
Warning (2): Cannot modify header information - headers already sent by /cake/libs/debugger.php:686) [CORE/cake/libs/controller/controller.php, line 742]
I think I am getting SQL Error 1452 because the foreign key is either empty or missing. Correct?
What am I doing wrong or not doing at all? Any input is greatly appreciated. I am just trying to get better!! Thanks!
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)
Note: There is a reason my id fields are varchar!
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' )); ?>
Now with that said, these are the errors I get when I excute my script......
Warning (512): SQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails (`ilsa_slic`.`spreadsheets`, CONSTRAINT `spreadsheets_ibfk_1` FOREIGN KEY (`coverage_id`) REFERENCES `coverages` (`id`)) [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: INSERT INTO `spreadsheets` (`modified`, `created`) VALUES ('2013-01-04 09:19:05', '2013-01-04 09:19:05')
Warning (2): Cannot modify header information - headers already sent by /cake/libs/debugger.php:686) [CORE/cake/libs/controller/controller.php, line 742]
I think I am getting SQL Error 1452 because the foreign key is either empty or missing. Correct?
What am I doing wrong or not doing at all? Any input is greatly appreciated. I am just trying to get better!! Thanks!