Skip to content

Commit

Permalink
Merge pull request #74 from cnizzardini/add-after-save-option-cake5
Browse files Browse the repository at this point in the history
Adds afterSave and resolves deprecations in test suite - cake 5.x
  • Loading branch information
dereuromark authored Jan 10, 2024
2 parents a912fda + 9ed548f commit 7068719
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,20 @@ The configuration contains the fully namespaced class name of your persister.
### Working With Transactional Queries

Occasionally, you may want to wrap a number of database changes in a transaction, so that it can be rolled back if one
part of the process fails. In order to create audit logs during a transaction, some additional setup is required. First
create the file `src/Model/Audit/AuditTrail.php` with the following:
part of the process fails. There are two ways to accomplish this. The easiest is to change your save strategy to use
`afterSave` instead of `afterCommit`. In your applications configuration, such as `config/app.php`:

```php
'AuditStash' => [
'saveType' => 'afterSave',
]
```

That's it if you use afterSave. You should read up on the difference between the two as there are drawbacks:
https://book.cakephp.org/4/en/orm/table-objects.html#aftersave

If you are using the default afterCommit, in order to create audit logs during a transaction, some additional setup is
required. First create the file `src/Model/Audit/AuditTrail.php` with the following:

```php
<?php
Expand Down
16 changes: 15 additions & 1 deletion src/Model/Behavior/AuditLogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,21 @@ class AuditLogBehavior extends Behavior
*/
public function implementedEvents(): array
{
return [
$events = [
'Model.beforeSave' => 'injectTracking',
'Model.beforeDelete' => 'injectTracking',
'Model.afterSave' => 'afterSave',
'Model.afterDelete' => 'afterDelete',
'Model.afterSaveCommit' => 'afterCommit',
'Model.afterDeleteCommit' => 'afterCommit',
];

if (Configure::read('AuditStash.saveType') !== 'afterSave') {
$events['Model.afterSaveCommit'] = 'afterCommit';
$events['Model.afterDeleteCommit'] = 'afterCommit';
}

return $events;
}

/**
Expand Down Expand Up @@ -138,6 +145,10 @@ public function afterSave(
}

$options['_auditQueue']->attach($entity, $auditEvent);

if (Configure::read('AuditStash.saveType') === 'afterSave') {
$this->afterCommit(new Event(''), $entity, $options);
}
}

/**
Expand Down Expand Up @@ -190,6 +201,9 @@ public function afterDelete(
$primary = $entity->extract((array)$this->_table->getPrimaryKey());
$auditEvent = new AuditDeleteEvent($transaction, $primary, $this->_table->getTable(), $parent);
$options['_auditQueue']->attach($entity, $auditEvent);
if (Configure::read('AuditStash.saveType') === 'afterSave') {
$this->afterCommit(new Event(''), $entity, $options);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Model/Behavior/AuditIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AuditStash\Model\Behavior\AuditLogBehavior;
use AuditStash\PersisterInterface;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;

class DebugPersister implements PersisterInterface
Expand All @@ -35,6 +36,9 @@ class AuditIntegrationTest extends TestCase
'core.ArticlesTags',
];

private ?Table $table;
private mixed $persister;

/**
* tests setup.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class AuditLogBehaviorTest extends TestCase
{
private ?Table $table;
private ?AuditLogBehavior $behavior;

public function setUp(): void
{
parent::setUp();
Expand Down

0 comments on commit 7068719

Please sign in to comment.