CakePHP Burzum /文件存储无法保存文件

时间:2016-05-01 14:38:16

标签: cakephp cakephp-3.0 file-storage

我安装了"burzum/file-storage": "^1.1"并按照文档

将此添加到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);

我有一张名为评论的表格,并从文档中添加了

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

添加了此表单:

<?= $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() ?>

使用

将表格添加到我的数据库中
cake migrations migrate -p Burzum/FileStorage

发送表单时,文件不会保存在 TMP 文件夹中,也不会保存在数据库中。

我尽力坚持事件。我不想使用手册版本(这适用于btw)。

我错过了什么?

最好的问候 Sundypha

1 个答案:

答案 0 :(得分:0)

如果您仍需要帮助,我会详细介绍。

您需要创建另一个表格,我的文章可能是您自己的评论文章

<?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);

}
    }
    ?>

//在我的文章表中我有

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

//我的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);
}