On Thu, Jan 3, 2013 at 5:33 PM, Karl Smith <barnunboi@hotmail.com> wrote:
> Hello,
> I am having some trouble saving an uploaded file to a table in MySQL
> database.
You don't say what the trouble is. The record isn't being saved?
> 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' => ''
> );
Can a Coverage have only ONE Spreadsheet? Does the spreadsheets table
have a coverage_id column?
> Also I created a model called Spreadsheet and it is as follows....
>
>
> public $name = 'Spreadsheet';
You can leave out $name if you're using PHP 5.x
> public $hasone = 'Coverage';
It should be $hasOne. Except in this case you don't want this here
because it should be in $belongsTo.
(In certain circumstances, you can have a model in both, but not using
the same alias.)
> public $belongsTo = array(
> 'Coverage' => array(
> 'className' => 'Coverage',
> 'foreignKey' => 'coverage_id'
> ),
> 'User' => array(
> 'className' => 'User',
> 'foreignKey' => 'user_id',
>
> )
> );
>
> };
>
> MySQL database table is as follows
Assuming the spreadsheets table.
> id(varchar)
> filename(varchar)
> file(mediumblob)
> user_id(varchar)
> coverage_id(varchar)
> created(datetime)
> modified(datetime)
id should be INT NOT NULL AUTO_INCREMENT
coverage_id & user_id should be INT NOT NULL
Also, saving files as BLOBs isn't a great idea. You'd be better off
saving the file to the filesystem and storing the path (optionally,
size, type, etc.) in the DB. But leave it as is for now until you can
get the records to save.
>
> This is my model I use to process and save data from the uploaded file:
>
> public function saveSpreadsheet($uploadedFile) {
Place this right at the beginning of the method:
die(debug($uploadedFile));
> $coverageId = $this->id;
Does $this->id have a value?
> exit();
Remove the exit() call.
> 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' )); ?>
You're not including the Coverage.id nor User.id. Normally, these
would be hidden form fields. However, if you're at all concerned about
someone screwing with the inputs, you can add these to the data later.
I don't see User.id added anywhere, and it's questionable whether
$this->id is set in the Coverage model method.
--
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.
Thursday, January 3, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment