cakephp-file-storage快速入门指南入门 [英] Getting Started with cakephp-file-storage quickstart guide

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

问题描述

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

按照教程,

三个表产品图片和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不是Assosicated ProductImages表好,你看到它在那里在教程,它列为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.

我会共享我的代码,但我只是从quickstart中复制

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

推荐答案

我最近开始使用CakePHP 3(我也有一点麻烦),首先我使用本地存储,那么这将是适当的设置

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


\xampp\htdocs\ [ProjectFolder] \config\bootstrap.php

C:\xampp\htdocs\[ProjectFolder]\config\bootstrap.php



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

将此块放在使用块下(请记住使用Burzum lib use Burzum\FileStorage\\ \\ Lib \StorageManager;

Put this block below use block (remember to use Burzum lib use Burzum\FileStorage\Lib\StorageManager;)

use Burzum\FileStorage\Lib\StorageManager;
use Cake\Cache\Cache;
use Cake\Console\ConsoleErrorHandler;
use Cake\Core\App;
use Cake\Core\Configure;

此行可以根据您的需要进行调整>

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

'adapterOptions' => [TMP, true],

(不必等于此)

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

这是MySql中的表格(只有两个表媒体 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' strong>(我不记得它是否可选,但我把它)。

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

我的上传方法 php

My upload method in 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!'));
        }
    }
}

组件(可选)中存储文件

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

并将方法负责 组件中的 路径

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']; code>,否则您将无法访问以表单(包括图片文件)发送的信息。

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

注意:我使用 XAMPP CakePHP 3

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆