Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Watcher Laravel Package

bentools/doctrine-watcher

Monitor Doctrine entity inserts and updates with a lightweight event subscriber. Watch specific classes and properties, get a changeset with old/new values or additions/removals, and run callbacks to react to changes (e.g., email or roles updates).

View on GitHub
Deep Wiki
Context7

Latest Stable Version License Build Status Quality Score Total Downloads

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky