Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 92b42d0

Browse files
author
Florian Krämer
committed
Documentation updates.
1 parent 11aa4e6 commit 92b42d0

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

docs/Documentation/How-To-Use.md

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Preparing the File Upload
1414

1515
This section is going to show how to store a file using the Storage Manager directly.
1616

17-
For example you have a Report model and want to save a pdf to it, you would then create an association like:
17+
For example you have a Report table and want to save a pdf to it, you would then create an association like:
1818

1919
```php
2020
public function initialize(array $config)
@@ -29,48 +29,21 @@ public function initialize(array $config)
2929
}
3030
```
3131

32-
In your `add.ctp` or `edit.ctp` views you would add something like:
32+
In your `add.ctp` or `edit.ctp` views you would add something like this.
3333

3434
```php
35+
echo $this->Form->create($report, ['type' => 'file']);
3536
echo $this->Form->input('title');
36-
echo $this->Form->input('pdf_files.file');
37+
echo $this->Form->file('pdf_files.file'); // Pay attention here!
3738
echo $this->Form->input('description');
39+
echo $this->Form->submit(__('Submit'));
40+
echo $this->Form->end();
3841
```
3942

40-
Handling the File Upload
41-
------------------------
43+
[Make sure your form is using the right HTTP method](http://book.cakephp.org/3.0/en/views/helpers/form.html#changing-the-http-method-for-a-form)!
4244

43-
**Now comes the crucial point of the whole implementation**
44-
45-
Because of to many different requirements and personal preferences out there the plugin is *not* automatically storing the file. You'll have to customize it a little but its just a matter for a few lines.
46-
47-
Lets go by this scenario inside the report model, assuming there is an add() method:
48-
49-
```php
50-
$entity = $this->newEntity($postData);
51-
$saved = $this->save($entity);
52-
if ($saved) {
53-
$key = 'your-file-name';
54-
if (StorageManager::adapter('Local')->write($key, file_get_contents($this->data['pdf_files']['file']['tmp_name']))) {
55-
$postData['pdf_files']['foreign_key'] = $saved->id;
56-
$postData['pdf_files']['model'] = 'Reports';
57-
$postData['pdf_files']['path'] = $key;
58-
$postData['pdf_files']['adapter'] = 'Local';
59-
$this->PdfDocuments->save($this->PdfDocuments->newEntity($postData));
60-
}
61-
}
62-
```
63-
64-
Later, when you want to delete the file, for example in the beforeDelete() or afterDelete() callback of your Report model, you'll know the adapter you have used to store the attached PdfFile and can get an instance of this adapter configuration using the StorageManager. By having the path or key available you can then simply call:
65-
66-
```php
67-
StorageManager::adapter($data['PdfFile']['adapter'])->delete($data['PdfFile']['path']);
68-
```
69-
70-
Insted of doing all of this in the model that has the files associated to it you can also simply extend the FileStorage model from the plugin and add your storage logic there and use that model for your association.
71-
72-
How to store an uploaded file II - using Events
73-
-----------------------------------------------
45+
Store an uploaded file using Events
46+
-----------------------------------
7447

7548
The **FileStorage** plugin comes with a class that acts just as a listener to some of the events in this plugin. Take a look at [LocalImageProcessingLister.php](../../Event/LocalImageProcessingLister.php).
7649

@@ -105,6 +78,42 @@ Event Listeners
10578

10679
See [this page](Included-Event-Listeners.md) for the event listeners that are included in the plugin.
10780

81+
82+
Handling the File Upload Manually
83+
---------------------------------
84+
85+
You'll have to customize it a little but its just a matter for a few lines.
86+
87+
Note the Listener expects a request data key `file` present in the form, so use `echo $this->Form->input('file');` to allow the Marshaller pick the right data from the uploaded file.
88+
89+
Lets go by this scenario inside the report table, assuming there is an add() method:
90+
91+
```php
92+
public function add() {
93+
$entity = $this->newEntity($postData);
94+
$saved = $this->save($entity);
95+
if ($saved) {
96+
$key = 'your-file-name';
97+
if (StorageManager::adapter('Local')->write($key, file_get_contents($this->data['pdf_files']['file']['tmp_name']))) {
98+
$postData['pdf_files']['foreign_key'] = $saved->id;
99+
$postData['pdf_files']['model'] = 'Reports';
100+
$postData['pdf_files']['path'] = $key;
101+
$postData['pdf_files']['adapter'] = 'Local';
102+
$this->PdfDocuments->save($this->PdfDocuments->newEntity($postData));
103+
}
104+
}
105+
return $entity;
106+
}
107+
```
108+
109+
Later, when you want to delete the file, for example in the beforeDelete() or afterDelete() callback of your Report model, you'll know the adapter you have used to store the attached PdfFile and can get an instance of this adapter configuration using the StorageManager. By having the path or key available you can then simply call:
110+
111+
```php
112+
StorageManager::adapter($data['PdfFile']['adapter'])->delete($data['PdfFile']['path']);
113+
```
114+
115+
Insted of doing all of this in the table object that has the files associated to it you can also simply extend the FileStorage table from the plugin and add your storage logic there and use that table for your association.
116+
108117
Why is it done like this?
109118
-------------------------
110119

0 commit comments

Comments
 (0)