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.
Thre
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
In file bootstrap.php
C:\xampp\htdocs\[ProjectFolder]\config\bootstrap.php
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem']
);
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],
to (not necessary equals this)
'adapterOptions' => [ROOT . DS . 'PicturesResources' . DS],
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)
);
I run cake bake all products and cake bake all medias and the result:
In 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'
]);
}
I add 'className' => 'Medias', (I don't remember if its optional but I put it).
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'
]);
}
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!'));
}
}
}
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 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();
?>
Notes: I'm using XAMPP and CakePHP 3