Access to your doctrine events from the Symfony DIC.
PHP
Latest commit 2a10372 Jun 9, 2016 @PedroTroller PedroTroller Merge pull request #8 from KnpLabs/fix/unstable-version
Update branch alias

README.md

Rapid Application Development : Doctrine Events

Access to your doctrine events from the Symfony DIC.

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

Installation

composer require knplabs/rad-doctrine-event ~2.1

And with Symfony:

class AppKernel
{
    function registerBundles()
    {
        $bundles = array(
            //...
            new Knp\Rad\DoctrineEvent\Bundle\DoctrineEventBundle(),
            //...
        );

        //...

        return $bundles;
    }
}

Usages

Context

Let's say you have the following entity:

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class User
{
    //...
}

Before

In order to plug doctrine events for that entity, we usually do:

namespace App\EventListener;

use App\Entity\User;
use Doctrine\ORM\Event\LifecycleEventArgs;

class UserListener
{
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if (false === $entity instanceof User) {
            return;
        }

        // Some stuff
    }
}
#services.yml
services:
    app.event_listener.user_listener:
        class: App\EventListener\UserListener
        tags:
            - { name: doctrine.event_listener, event: pre_persist, method: prePersist }

After

But with the KnpRadDoctrineEvent you will need:

namespace App\EventListener;

use Knp\Rad\DoctrineEvent\Event\DoctrineEvent;

class UserListener
{
    public function prePersist(DoctrineEvent $event)
    {
        $entity = $event->getEntity();

        // Some stuff
    }
}
#services.yml
services:
    app.event_listener.user_listener:
        class: App\EventListener\UserListener
        tags:
            - { name: kernel.event_listener, event: app.entity.user.pre_persist, method: prePersist }

Inheritance

Context

Let's say you have an entity extending another entity:

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"page" = "App\Entity\Customer"})
 */
class User
{
    //...
}
namespace App\Entity;

use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Customer extends User
{
    //...
}

Events

The parent entity events are dispatched just before the children entities:

For First event Second Event
pre_persist app.entity.user.pre_persist app.entity.customer.pre_persist
post_update app.entity.user.pre_update app.entity.customer.pre_update
...

Terminate

Each post (post_persist, post_update, post_remove, post_load) event is also redispatched during the kernel.terminate event.

Event Terminate event
app.entity.user.post_persist app.entity.user.post_persist_terminate
app.entity.user.post_update app.entity.user.post_update_terminate
app.entity.user.post_remove app.entity.user.post_remove_terminate
app.entity.user.post_load app.entity.user.post_load_terminate

Configuration

You can restrict event re-dispatching to specific entities.

You just have to follow this configuration:

knp_rad_doctrine_event:
    entities:
        - MyBundle\Entity\User

Then events will be dispatched only for the entity MyBundle\Entity\User.