FriendsOfCake / cakephp-upload

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

File not uploading/object not removed from data #586

Closed tbelknapsgs closed 2 years ago

tbelknapsgs commented 2 years ago

This was working as recently as April and for months before that. I can 100% believe I've done something wrong, but going over the installation instructions, I see nothing wrong. The long and short of it is: when I upload a file, the upload never happens and I receive a database error message because the "thumb" field (my file input) is not provided. Therefore, the query looks like this:

INSERT INTO images (name, description) 
VALUES 
  ('lsdjf', 'sldkjf')

However, the Image entity does not show any errors when the page reloads. I tried to catch the error this way, but it's not providing any additional information:

   /**
     * Generic record-saving function, to avoid duplicate code.
     *
     * @param \Cake\Datasource\EntityInterface $image The image object
     * @param string $action The action to redirect to.
     * @return \Cake\Http\Response|null
     */
    private function saveRecord(EntityInterface $image, $action = 'view')
    {
        $record = $this->Images->patchEntity(
            $image,
            $this->request->getData(),
            ['associated' => ['ImageOptions']]
        );
        $this->Authorization->authorize($image, 'edit');
        try {
            $this->Images->save($record);
            if ($this->saveMetadata($record)) {
                $this->Flash->success(__('The images has been saved.'));

                return $this->redirect(['action' => $action, $record->id]);
            }
        } catch (\Exception $e) {
            debug($e);
            debug($record);
            $this->Flash->error(__('The image could not be saved. Please try again.'));
            $message = new MessageDto([
                'subject' => 'Error saving image',
                'message' => print_r($record->getErrors(), true),
                'messageSent' => new FrozenTime('now'),
            ]);
            $this->Notification->sendSystem($message);
        }

        return null;
    }

But the debug() information doesn't show any errors either. Here are the values debugged to the screen:

APP/Controller/ImagesController.php (line 212)
object(PDOException) id:0 {
errorInfo => [ ]
queryString => 'INSERT INTO images (name, description) VALUES (:c0, :c1)'
protected message => 'SQLSTATE[HY000]: General error: 1364 Field 'thumb' doesn't have a default value'
protected code => 'HY000'
protected file => '/Users/thomasbelknap/one-vision/vendor/cakephp/cakephp/src/Database/Statement/MysqlStatement.php'
protected line => (int) 39
}
APP/Controller/ImagesController.php (line 213)
object(Visualize\Model\Entity\Image) id:0 {
'name' => 'lksjfd'
'description' => 'slkdjf'
'thumb' => object(Laminas\Diactoros\UploadedFile) id:1 { }
'url' => null
'[new]' => true
'[accessible]' => [ ]
'[dirty]' => [ ]
'[original]' => [ ]
'[virtual]' => [ ]
'[hasErrors]' => false
'[errors]' => [ ]
'[invalid]' => [ ]
'[repository]' => 'Images'
}
davidyell commented 2 years ago

What version of Cake and the plugin are you using?

Also, the upload is a behaviour so will need to be loaded onto the model, which I think in your case is the Images model.

tbelknapsgs commented 2 years ago

Sorry should have specified: CakePHP 4.4 Upload plugin 5.0.1

I figured out the issue, thank you.