Allows to track changes on doctrine entities with an easy-to-use and highly customizable API.
MIT License
Allows to track changes on doctrine entities with an easy-to-use and highly customizable API.
You can use this library to track changes to Doctrine Entities. Use annotations to define the fields that you want to monitor. They determine where the changes are to be saved.
// User Entity class
/**
* @Column(type="string")
* @WatchedField // <-- Watcher now tracks changes related to this field
*/
protected string $emailAddress;
/**
* @var $dbParams array
* @var $config Configuration
*/
$em = EntityManager::create($dbParams, $config, Watcher::createEventManager(new DatabaseHandler()));
Watcher::registerAnnotations();
/** @var $user User */
$user = $em->getRepository(User::class)->find(1);
$user->setUsername("A new username");
$em->persist($user);
$em->flush();
/** @var EntityLogRepository $logRepo */
$logRepo = $em->getRepository(EntityLog::class);
/** @var EntityLog[] $changes */
$changes = $logRepo->getLogsFromEntity($user);
$lastChange = $changes[0];
echo vsprintf("Last updated at (%s): Changed %s from '%s' to '%s'", [
$lastChange->getChangedAt()->format('Y-m-d'),
$lastChange->getFieldLabel(),
$lastChange->getOldValue(),
$lastChange->getNewValue(),
]); // Last updated at 2017-09-07: Changed Email Address from '[email protected]' to '[email protected]'
watcher.db_handler:
class: App\Utils\AppWatcherHandler #your handler, e.g. Database Handler
autowire: true
Watcher\EventListener\FlushListener:
calls:
- method: pushUpdateHandler
arguments:
- '@watcher.db_handler'
- method: setAnnotationReader
arguments:
- '@annotations.reader'
tags:
- { name: doctrine.event_listener, event: onFlush }
Watcher\EventListener\LoadListener:
arguments:
- 'App\Entity\EntityLog' # The entity which relates to EntityLogRepository
tags:
- { name: doctrine.event_listener, event: postLoad }
@OneToMany
) are NOT supported. @ManyToMany
and @ManyToOne
associations are supported.TBD (support is appreciated!)
Bugs and feature request are tracked on GitHub.
This library depends on Doctrine (surprise!) and subpackages.
Watcher is licensed for use under the MIT License (MIT). Please see LICENSE for more information.