Skip to content

Commit f7f8894

Browse files
committed
Added attachments to README
1 parent 2c33ead commit f7f8894

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,49 @@ Both the `Model` and `KeyCollection` classes implemement the `JsonSerializable`
256256
echo $pace->customer->read('HOUSE');
257257
```
258258

259+
## Attachments
260+
261+
### Attaching files
262+
263+
To attach a file to a model, you only need to specify its name and content. The library takes care of guessing the MIME type and encoding the content.
264+
265+
```php
266+
$job = $pace->model('Job')->read('12345');
267+
$attachment = $job->attachFile('test.txt', file_get_contents('test.txt'));
268+
$attachment->description = 'A test file';
269+
$attachment->save();
270+
```
271+
272+
The `attachFile()` method returns a FileAttachment model so you can set the category, description, etc. Only call the `save()` method if you change any attributes.
273+
274+
You can also attach a file to a field by specifying the name of the field. This is used throughout Pace for logos, photos, layouts, etc.
275+
276+
```php
277+
$company = $pace->model('Company')->read('001');
278+
$company->attachFile('logo.png', file_get_contents('logo.png'), 'logo');
279+
```
280+
281+
### Retrieving attached files
282+
283+
Files attached to a model are retrieved via a special relationship method. It behaves the same as a "has many" relationship and returns an `XPath\Builder` instance.
284+
285+
```php
286+
$attachments = $job->fileAttachments()->get();
287+
```
288+
289+
Use a `filter()` to limit the results to one field or one type of file.
290+
291+
```php
292+
$logo = $company->fileAttachments()->filter('@field', 'logo')->first();
293+
$spreadsheets = $job->fileAttachments()->filter('@ext', 'xls')->get();
294+
```
295+
296+
Finally, call `getContent()` on a FileAttachment model to read the content of the file. The library automatically decodes the content for you.
297+
298+
```php
299+
$attachment->getContent();
300+
```
301+
259302
## Version
260303

261304
Identify the version of Pace running on the server.

0 commit comments

Comments
 (0)