Sunday, February 13, 2022

[FIXED] CakePHP Burzum/file-storage not able to save files

Issue

I installed the "burzum/file-storage": "^1.1" and followed the docs

added this to bootstrap.php:

use Cake\Event\EventManager;
use Burzum\FileStorage\FileStorageUtils;
use Burzum\FileStorage\Storage\StorageManager;
use Burzum\FileStorage\Event\LocalFileStorageListener;


StorageManager::config('Local', [
    'adapterOptions' => [TMP, true],
    'adapterClass' => '\Gaufrette\Adapter\Local',
    'class' => '\Gaufrette\Filesystem'
]);

$listener = new LocalFileStorageListener();
EventManager::instance()->on($listener);

I've got a table called Comments and added this from the Docs

$this->hasOne('PdfFiles', [
        'className' => 'Burzum/FileStorage.PdfFiles',
        'foreignKey' => 'foreign_key',
        'conditions' => [
            'PdfFiles.model' => 'Comments'
        ]
    ]);

added this form:

<?= $this->Form->create($comment, ['type' => 'file']) ?>
<fieldset>
    <legend><?= __('Edit Comment') ?></legend>
    <?php
        echo $this->Form->file('pdf_files.file');
        echo $this->Form->input('comment');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

added the table to my database with

cake migrations migrate -p Burzum/FileStorage

When I send the form the file will not be saved in the TMP folder and not in the database.

I try to stick to events. I don't want to use the manual version (this works btw).

What am I missing?

best regards Sundypha


Solution

I'll go into details if you still need help with this.

You need to create another table mine is articlesImages yours might be CommentDocs

<?php

namespace App\Model\Table;
use Burzum\FileStorage\Model\Table\ImageStorageTable;
class ArticleImagesTable extends ImageStorageTable {


public $actsAs = array(
    'FileStorage.UploadValidator' => array(
        'allowedExtensions' => array(
            'jpg',
            'jpeg',
            'png'
        ),
    ),
);
public function upload($article_id, $entity) {

    $entity = $this->patchEntity($entity, [
        'adapter' => 'Local',
        'model' => 'Articles',
        'foreign_key' => $article_id,
    ]);
    return $this->save($entity);

}
    }
    ?>

//in my articles table i have

$this->hasMany('ArticleImages', [
            'foreignKey' => 'foreign_key',
           'conditions' => [
            'model' => 'Articles'
           ]
         ]);

//my ArticlesController

public function upload($id = null) {

    $entity = $this->Articles->ArticleImages->newEntity();
    if ($this->request->is(['post', 'put'])) {
        $entity = $this->Articles->ArticleImages->patchEntity($entity,$this->request->data);

        if ($this->Articles->ArticleImages->upload($id, $entity)) {
            $this->Flash->set(__('Upload successful!'));
        }            
    }
    $this->set('ArticleImage', $entity);
}


Answered By - artSir

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.