如何使用Cakephp-File-Storage保存图像?

时间:2015-10-28 04:41:00

标签: php cakephp cakephp-3.0 file-storage

更新

所以我在ProductsController的动作上传中添加了一些日志,在MediasTable中添加了方法上传以查明发生了什么。 ProductsController this->Products->Medias->newEntity()中的实体已传递给MediasTable,但它没有保存。

enter image description here

有必要上传文件以保存数据库中的数据吗?就像所有数据都没问题但文件不存在一样,事件将拒绝数据并在数据库中什么都不做?

我使用cakephp 3.1和文件存储插件。我按照文档中的快速入门指南进行操作:Quick-Start但我不了解某些部分且无法上传,在数据库中插入也不会缩略图。

这是我的数据库:

CREATE TABLE products (
   id INT AUTO_INCREMENT PRIMARY KEY,
   product_name VARCHAR(255) NOT NULL,
   quantity INT NOT NULL,
   sold INT NOT NULL,
   description VARCHAR(1000),
   price DECIMAL(7,2) NOT NULL,
   old_price DECIMAL(7,2) NOT NULL,
   visited INT NOT NULL,
   status INT NOT NULL,
   created DATETIME,
   modified DATETIME
);

CREATE TABLE media_types (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name_media_type VARCHAR(255) NOT NULL,
   created DATETIME,
   modified DATETIME
);

 CREATE TABLE medias (
  id INT AUTO_INCREMENT PRIMARY KEY,
  media_type_id INT NOT NULL,
  product_id INT NOT NULL,
  path VARCHAR(255) NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY media_type_key (media_type_id) REFERENCES media_types(id),
  FOREIGN KEY product_key (product_id) REFERENCES products(id)
);

MediasTable:

...
use Burzum\FileStorage\Model\Table\ImageStorageTable;

class MediasTable extends ImageStorageTable {
    public function initialize(array $config) {
        parent::initialize($config);
        $this->table('medias');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('MediaTypes', [
            'foreignKey' => 'media_type_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Products', [
            'foreignKey' => 'product_id',
            'joinType' => 'INNER'
        ]);
    }
    ...
    public function upload($productId, $entity) {
        $media = $this->patchEntity($entity, [
            'adapter' => 'Local',
            'model' => 'Media',
            'foreign_key' => $productId
        ]);
        Log::write('debug', $media);
        return $this->save($media);
    }
}

ProductsTable:

class ProductsTable extends Table {
    public function initialize(array $config) {
        parent::initialize($config);
        $this->table('products');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Medias', [
            'className' => 'Medias',
            'foreignKey' => 'foreign_key',
            'conditions' => [
                'Medias.model' => 'Media'
           ]
        ]);
    }
    ...
}

的ProductsController:

class ProductsController extends AppController {
    public function upload($productId = null) {
        $productId = 2;
        $entity = $this->Products->Medias->newEntity();
        if ($this->request->is(['post', 'put'])) {
            $entity = $this->Products->Medias->patchEntity(
                $entity,
                $this->request->data
            );
            if ($this->Products->Medias->upload($productId, $entity)) {
                $this->Flash->set(__('Upload successful!'));
            }
        }
        $this->set('productImage', $entity);
    }
    ...
}

在config / local_store.php中与示例相同(我在boostrap.php中包含此文件)

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

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

Configure::write('FileStorage', [
'imageSizes' => [
    'Medias' => [
        'large' => [
                ...
]);

FileStorageUtils::generateHashes();

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

upload.ctp

echo $this->Form->create($productImage, array('type' => 'file'));
echo $this->Form->file('file');
echo $this->Form->error('file');
echo $this->Form->submit(__('Upload'));
echo $this->Form->end();

在快速入门中有两种上传方法:uploadImage和uploadDocument 但是在控制器中他们使用"上传"。

示例中的产品中还有另一个关联,我需要这个吗?:

        $this->hasMany('Documents', [
        'className' => 'FileStorage.FileStorage',
        'foreignKey' => 'foreign_key',
        'conditions' => [
            'Documents.model' => 'ProductDocument'
        ]
    ]);

我发现了这个问题(从那里开始使用我的数据库)Getting Started with cakephp-file-storage quickstart guide并上传和插入,但没有制作缩略图,如果我将表格更改为ImageStoreTable则显示错误& #34;未找到课程"

所以,如果有人能帮助我,我将非常感激!

0 个答案:

没有答案