Skip to content

Allows to track changes on doctrine entities with an easy-to-use and highly customizable API.

License

Notifications You must be signed in to change notification settings

dsentker/DoctrineWatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Watcher

GitHub release Packagist Maintenance

Allows to track changes on doctrine entities with an easy-to-use and highly customizable API.

Documentation

View Documentation

Quick example

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 'foo@example.com' to 'foo42@example.com' 

Symfony4 services.yaml integration (example)

    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 }

Known Limitations

  • This package is able to track changes on single fields and associations (collections), but depends on the concept of Doctrine, which is limited to track changes on fields on the owning side. That means, that inverse side associations (@OneToMany) are NOT supported. @ManyToMany and @ManyToOne associations are supported.
  • Also consider the overhead. If you've chosen the DatabaseHandler, each tracked entity change results in a single database request.

Testing

TBD (support is appreciated!)

Credits

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub.

ToDo

  • Use interfaces for everything
  • Easy Symfony integration
  • Write tests
  • Optimize performance (group changes?)

External Libraries

This library depends on Doctrine (surprise!) and subpackages.

Copyright and license

Watcher is licensed for use under the MIT License (MIT). Please see LICENSE for more information.