FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Upload field is empty when saving to database #469

Closed fcamargo10 closed 7 years ago

fcamargo10 commented 7 years ago

With this new version i try and not work.

Model

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'photo',
]);

Controller

 if ($this->request->is('post')) {
            $document = $this->Documents->patchEntity($document, $this->request->getData());
            $document->user_id = $this->Auth->user('id');

            if ($this->Documents->save($document)) {
                $this->Flash->success(__('The document has been sended.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The document could not be sended. Please, try again.'));
}

View

 echo $this->Form->control('photo', ['type' => 'file', 'required' => 'true']);

I already put the form type as file.

The error is: Error: SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value

When i comment the behavior everything work's fine. Anyone can help me ?

Spriz commented 7 years ago

That sure looks like an SQL error for your table field called "photo" :-)

Could you try to debug internally if something is getting set to null for some reason?

davidyell commented 7 years ago

I'm not sure how your code is managing to save a Document with an empty photo field, but that's why you're seeing the error. So some PHP validation here would help catch this in PHP, where it will be easier to handle.

fcamargo10 commented 7 years ago

So you are attaching the behaviour to the DocumentsTable right?

Right!

The form type is ['type' => 'file']

Yes

You are validating the field is notEmpty() and requirePresence() in your DocumentsTable validation?

Yes

If i comment the behavior works fine.

fcamargo10 commented 7 years ago

The problem has been fixed. Thanks guys.

justcharlz commented 7 years ago

@filipecamargo10 wondering how you fixed the problem. Currently facing same issue.

josegonzalez commented 7 years ago

You need to ensure your form and field type are both file. This is almost always missed. Please do so, but if you have any other issues, file a separate bug. Thanks.