我安装了"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);我有一个名为注释的表,并从Docs中添加了这个
$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文件夹中,而不会保存在数据库中。
我试着专注于事件。我不想使用手动版本(顺便说一句)。
我遗漏了什么?
向太阳报问好
发布于 2017-03-22 02:19:28
如果你还需要帮助的话,我会详细说明的。
您需要创建另一个表,我的表是articlesImages,您的表可能是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);
}
}
?>//在我的文章表中
$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);
}https://stackoverflow.com/questions/36967844
复制相似问题