cakephp-file-storage 快速入门指南

时间:2023-01-18
本文介绍了cakephp-file-storage 快速入门指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

https://github.com/burzum/cakephp-file-storage/blob/3.0/docs/Tutorials/Quick-Start.md

按照教程,要么全搞砸了,要么我全搞砸了.

Following the tutorial, it's all messed up or I am all messed up.

三个表 Products Images 和 ProductImages 只是试图捕捉任何工作.

Three tables Products Images and ProductImages just trying to catch anything working.

upload.ctp productImage 中的第一个错误未定义.嗯,我在控制器中看到它没有设置.老实说,我不知道在添加表单中传递它的目的.我知道编辑它会填充数据.

Well first error in the upload.ctp productImage is not defined. Well duh, I see it in the controller its not set. I'm honestly don't know the purpose in passing that in the form create on an add. I know for an edit it will populate the data.

下一个错误当我提交时,我收到一个错误 ProductTable is not assicated to ProductImages table,你在教程中看到它,它被列为 hasMany 'Images'.

Next error When I submit i get an error ProductTable is not assosicated to ProductImages table well you see it right there in the tutorial, its listed as hasMany 'Images'.

所以我把它改成 ProductImagesTable

So i change it to ProductImagesTable

并且我收到一个错误,即上传未定义,我假设它们是从控制器引用的,并且它被 ImageStorageTable 继承,我将其更改为 uploadImage 只是为了避免出现错误

And I get an error that upload is not defined, I'm assuming they are referencing from the controller and that its being inherited by the ImageStorageTable I changed it to uploadImage just trying not to get an error

我只是想了解一下它是如何工作的.如果有人可以与我分享这个工作的项目.我可以破译什么是错的.

I am just trying to get some kind of 'hello world' on how this works. If someone can just share me a project with this working. I can decipher what is wrong.

我想分享我的代码,但我只是从快速入门中复制了

I would share my code but I just copied from the quickstart

推荐答案

我最近也开始使用 CakePHP 3(我也遇到了一些麻烦),首先我使用的是 Local Storage那么这将是适当的设置

I recently started using CakePHP 3 too (I also had a bit of trouble), first of all I am using a Local Storage then this would be the appropriate setting

在文件 bootstrap.php

C:xampphtdocs[ProjectFolder]configootstrap.php

C:xampphtdocs[ProjectFolder]configootstrap.php

StorageManager::config('Local', [
    'adapterOptions' => [TMP, true],
    'adapterClass' => 'GaufretteAdapterLocal',
    'class' => 'GaufretteFilesystem']
);

把这个块放在use块下面(记得使用Burzum lib use BurzumFileStorageLibStorageManager;)

Put this block below use block (remember to use Burzum lib use BurzumFileStorageLibStorageManager;)

use BurzumFileStorageLibStorageManager;
use CakeCacheCache;
use CakeConsoleConsoleErrorHandler;
use CakeCoreApp;
use CakeCoreConfigure;

这一行可以根据您的需要进行调整(有存储文件的文件夹).

This line can be adjusted to your needs (had the Folder where file be storage).

'adapterOptions' => [TMP, true],

到(不需要等于这个)

'adapterOptions' => [ROOT . DS . 'PicturesResources' . DS],

这是我在 MySql 中的表(只有两个表 productsmedias 存储图像路径(media_types 对这个例子不重要))

This is my tables in MySql (Only two tables products and medias that store image paths (media_types is not important to this example))

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

我运行蛋糕烘焙所有产品蛋糕烘焙所有媒体,结果:

ProductsTable.php

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' => 'product_id'
    ]);
}

我添加了 'className' => 'Medias',(我不记得它是否是可选的,但我把它).

I add 'className' => 'Medias', (I don't remember if its optional but I put it).

MediasTable.php 与bake 生成的相同.

The MediasTable.php are the same generated by bake.

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'
    ]);
}

我在ProductsController.php

 public function upload() {

    if ($this->request->is('post')) {
        $mediaTypeId = 1;
        $productId = 2;
        $path = $this->request->data['Media']['file']['tmp_name'];
        $inserted = $this->Insert->insertMedia($mediaTypeId, $productId, $path);

        //-------------------------------------------------------------------------

        $stringSeparator = '_';
        $storeName = 'StoreGYN';
        $productName = 'TestProduct';
        $saved = $this->UploadFile->saveFileLFS($stringSeparator, $storeName,
            $productName);

        if($inserted === true && $saved === true){
            $this->Flash->set(__('Upload successful!'));
        }
    }
}

我将负责存储文件的方法放在组件中(它是可选的):

I put the method responsible to Store the file in a component (it's optional):

public function saveFileLFS($stringSeparator, $storeName, $productName)
{
    $key = $storeName . $stringSeparator . $productName . $stringSeparator .
        $this->request->data['Media']['file']['name'];
    if(StorageManager::adapter('Local')->write($key,
        file_get_contents($this->request->data['Media']['file']['tmp_name']))){
        return true;
    }else
    {
        return false;
    }
}

并将负责保存图像路径的方法也放在组件中:

And put the method responsible to Save the Path to Image in a component too:

public function insertMedia($mediaTypeId, $productId, $path)
{
    $media = TableRegistry::get('Medias')->newEntity();
    $media->media_type_id = $mediaTypeId;
    $media->product_id = $productId;
    $media->path = $path;

    if(TableRegistry::get('Medias')->save($media)){
        return true;
    }
    else{
        return false;
    }
}

这是模板,注意输入元素名称它们应该与键相同$this->request->data['Media']['file']['tmp_name']; 否则您将无法访问以表单(包括图像文件)发送的信息.

This is the template, pay atention in the input elements name they should be the same as the keys $this->request->data['Media']['file']['tmp_name']; otherwise you will not be able to access the information sent in form (Including Image File).

<?php
echo $this->Form->create(null, array(
    'type' => 'file'
));
echo $this->Form->file('Media.file');
echo $this->Form->error('file');
echo $this->Form->submit(__('Upload'));
echo $this->Form->end();
?>

注意:我使用的是 XAMPPCakePHP 3

Notes: I'm using XAMPP and CakePHP 3

这篇关于cakephp-file-storage 快速入门指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:CakePHP 使用多个数据库作为模型 下一篇:Cakephp 覆盖 HtmlHelper::link

相关文章

最新文章