我最近也开始使用CakePHP 3(我也遇到了一些麻烦),首先我正在使用本地存储,那么这将是适当的设置。
在文件bootstrap.php中:
C:\xampp\htdocs\[ProjectFolder]\config\bootstrap.php
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem']
);
把此代码块放在 use 代码块下面(记得使用 Burzum 库:
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;
这一行可以根据您的需求进行调整(文件所在的文件夹)。
'adapterOptions' => [TMP, true],
转化为(不一定等同于这个)
'adapterOptions' => [ROOT . DS . 'PicturesResources' . DS],
这是我在MySql中的表格(只有两个表格产品和媒体,存储图像路径(媒体类型对本示例不重要))
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)
);
我运行了cake bake all products和cake bake all medias,结果如下:
在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', (我不确定它是否是可选项,但我把它放进去了)。
MediasTable.php 与 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!'));
}
}
}
我将负责存储文件的方法放在了一个组件中(这是可选的):
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;
}
}
同时,将负责保存图像路径的方法也放到一个组件中:Save the Path to Image
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'];
相同,否则您将无法访问在表单中发送的信息(包括图像文件)。
<?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();
?>
Notes: I'm using XAMPP and CakePHP 3