bentools / doctrine-watcher
Watch changes in your Doctrine entities to let you trigger custom events.
Installs: 1 261
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- bentools/iterable-functions: ~1.0
- doctrine/orm: ~2.5
Requires (Dev)
- matthiasnoback/doctrine-orm-test-service-provider: ^3.0
- phpunit/phpunit: ^6.0
- satooshi/php-coveralls: @stable
- squizlabs/php_codesniffer: @stable
- symfony/var-dumper: ^4.1
This package is auto-updated.
Last update: 2022-06-14 12:04:18 UTC
README
Doctrine Watcher
This little library will help you to monitor changes on Doctrine insertions and/or updates, for specific classes, for specific properties.
Usage
use App\Entity\User; use BenTools\DoctrineWatcher\Changeset\PropertyChangeset; use BenTools\DoctrineWatcher\Watcher\DoctrineWatcher; /** * Instanciate watcher */ $watcher = new DoctrineWatcher(); /** * Register it as an event subscriber * @var \Doctrine\Common\EventManager $eventManager */ $eventManager->addEventSubscriber($watcher); /** * Watch for changes on the $email property for the User class */ $watcher->watch(User::class, 'email', function ( PropertyChangeset $changeset, string $operationType, User $user ) { if (!$changeset->hasChanges()) { return; } vprintf('Changed email from %s to %s for user %s' . PHP_EOL, [ $changeset->getOldValue(), $changeset->getNewValue(), $user->getName(), ]); }); /** * Watch for changes on the $roles property for the User class */ $watcher->watch(User::class, 'roles', function ( PropertyChangeset $changeset, string $operationType, User $user ) { if ($changeset::INSERT === $operationType) { return; } if ($changeset->hasAdditions()) { vprintf('Roles %s were granted for user %s' . PHP_EOL, [ implode(', ', $changeset->getAdditions()), $user->getName(), ]); } if ($changeset->hasRemovals()) { vprintf('Roles %s were revoked for user %s' . PHP_EOL, [ implode(', ', $changeset->getRemovals()), $user->getName(), ]); } });
Installation
PHP7.1+ is required.
composer require bentools/doctrine-watcher:0.2.*
Tests
./vendor/bin/phpunit
F.A.Q.
Can I also trigger callable on insertions ?
$watcher = new DoctrineWatcher(['trigger_on_persist' => true]); // Will be default config for all watchers
or
$watcher->watch(Entity::class, 'property', $callable, ['trigger_on_persist' => true]); // Will apply on this watcher only
How do I trigger something even when there are no changes?
$watcher = new DoctrineWatcher(['trigger_when_no_changes' => true]); // Will be default config
or
$watcher->watch(Entity::class, 'property', $callable, ['trigger_when_no_changes' => true]); // Will apply on this watcher only
When are the callback triggered?
On postPersist
and postUpdate
events.
License
MIT
See also
bentools/doctrine-watcher-bundle - A Symfony Bundle for this library