如何使用Cakephp文件存储保存图像?
更新 因此,我在ProductsController中的操作上载和MediasTable中的方法上载中添加了一些日志,以了解发生了什么。ProductsController如何使用Cakephp文件存储保存图像?,php,cakephp,cakephp-3.0,file-storage,Php,Cakephp,Cakephp 3.0,File Storage,更新 因此,我在ProductsController中的操作上载和MediasTable中的方法上载中添加了一些日志,以了解发生了什么。ProductsControllerthis->Products->Medias->newEntity()中的实体已传递给MediasTable,但未保存 是否需要上传文件以将数据保存在数据库中?例如,如果所有数据都正常,但文件不存在,则事件将拒绝数据并在数据库中不执行任何操作 我将CakePHP3.1与文件存储插件一起使用。我遵循文档中的快速入门指南:但我
this->Products->Medias->newEntity()
中的实体已传递给MediasTable,但未保存
是否需要上传文件以将数据保存在数据库中?例如,如果所有数据都正常,但文件不存在,则事件将拒绝数据并在数据库中不执行任何操作
我将CakePHP3.1与文件存储插件一起使用。我遵循文档中的快速入门指南:但我不了解某些部分,不上传、插入数据库,也不制作缩略图 这是我的数据库:
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'
]
]);
}
...
}
产品控制器:
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中)
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'
]
]);
我发现了这个问题(从我正在使用的数据库中)并上传和插入,但没有生成缩略图,如果我将表更改为ImageStoreTable,则会显示一个错误“找不到类”
因此,如果有人能帮助我,我将非常感激 快速看一下,这似乎是对的,但我不理解你的问题。您提供的描述基本上是“它不工作”。这无助于调试它。它会引发事件吗?桌子上写了什么东西吗?文件是否显示在服务器上?您是否调试了post数据。。。?我将使用教程代码创建一个示例应用程序,我将在未来几天内在Github上发布。为什么需要媒体和媒体类型表?文件存储旨在避免需要此类表。如果使用
媒体
表,插件将无法工作。我想这就是你在代码中所做的。嗨,burzum,我正在学习使用cakephp文件存储和stackoverflow中的另一个问题使用数据库。它可以上传文件,所以我一直使用它。在快速启动之后,我尝试调整数据库,但它没有上传文件(服务器中没有出现),没有显示flash消息,也没有向数据库写入任何内容。我将调试并稍后更新问题。非常感谢。
$this->hasMany('Documents', [
'className' => 'FileStorage.FileStorage',
'foreignKey' => 'foreign_key',
'conditions' => [
'Documents.model' => 'ProductDocument'
]
]);