knplabs / rad-doctrine-event
Access your doctrine events from the Symfony DIC.
Installs: 36 485
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 28
Forks: 3
Open Issues: 0
Requires
- php: ~7.0
- doctrine/inflector: ~1.0
- doctrine/orm: ~2.4
- symfony/config: ~2.0||~3.0
- symfony/dependency-injection: ~2.0||~3.0
- symfony/http-kernel: ~2.0||~3.0
Requires (Dev)
- knplabs/phpspec-welldone-extension: dev-master@dev
- phpspec/phpspec: ~2.4
This package is auto-updated.
Last update: 2022-09-23 13:37:18 UTC
README
Unfortunately we decided to not maintain this project anymore (see why). If you want to mark another package as a replacement for this one please send an email to hello@knplabs.com.
Rapid Application Development : Doctrine Events
Access to your doctrine events from the Symfony DIC.
Official maintainers:
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; } }
Use
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
.