Skip to content

Commit

Permalink
Docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 11, 2019
1 parent de57157 commit 64fc945
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/Behavior/AfterSave.md
@@ -0,0 +1,35 @@
# AfterSave Behavior

A CakePHP behavior to allow the entity to be available inside afterSave() callback.

## Introduction
It takes a clone of the entity from beforeSave(). This allows all the
info on it to be available in the afterSave() callback or from the outside without resetting (dirty, ...).

### Technical limitation
Make sure you do not further modify the entity in the table's beforeSave() then. As this would
not be part of the cloned and stored entity here.

## Usage
Attach it to your model's `Table` class in its `initialize()` method like so:
```php
$this->addBehavior('Tools.AfterSave', $options);
```

Then inside your table you can do:
```php
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) {
$entityBefore = $this->getEntityBeforeSave();
// Now you can check isDirty() etc
}
```

The same call could also be made from the calling layer/object on the table:
```php
$table->saveOrFail();
$entityBefore = $table->getEntityBeforeSave();
```

If you are using save(), make sure you check the result and that the save was successful.
Only call this method after a successful save operation.
Otherwise, there will not be an entity stored and you would get an exception here.
1 change: 1 addition & 0 deletions docs/README.md
Expand Up @@ -30,6 +30,7 @@ Controller:
* [Controller](Controller/Controller.md)

Behaviors:
* [AfterSave](Behavior/AfterSave.md)
* [Jsonable](Behavior/Jsonable.md)
* [Passwordable](Behavior/Passwordable.md)
* [Slugged](Behavior/Slugged.md)
Expand Down

0 comments on commit 64fc945

Please sign in to comment.