bentools / doctrine-watcher
此软件包已被废弃且不再维护。未建议替代软件包。
监视您的 Doctrine 实体中的更改,以触发自定义事件。
0.2
2018-11-26 13:37 UTC
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
这个小库可以帮助您监视 Doctrine 插入和/或更新中的更改,针对特定类,针对特定属性。
使用方法
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(), ]); } });
安装
需要 PHP7.1+。
composer require bentools/doctrine-watcher:0.2.*
测试
./vendor/bin/phpunit
常见问题解答
我可以在插入时也触发可调用的函数吗?
$watcher = new DoctrineWatcher(['trigger_on_persist' => true]); // Will be default config for all watchers
或者
$watcher->watch(Entity::class, 'property', $callable, ['trigger_on_persist' => true]); // Will apply on this watcher only
在没有更改的情况下,我该如何触发某个操作?
$watcher = new DoctrineWatcher(['trigger_when_no_changes' => true]); // Will be default config
或者
$watcher->watch(Entity::class, 'property', $callable, ['trigger_when_no_changes' => true]); // Will apply on this watcher only
回调函数何时被触发?
在 postPersist
和 postUpdate
事件上。
许可协议
MIT
另请参阅
bentools/doctrine-watcher-bundle - 为此库提供的 Symfony 扩展包