knplabs/rad-doctrine-event
最新稳定版本:v2.2.0
Composer 安装命令:
composer require knplabs/rad-doctrine-event
包简介
Access your doctrine events from the Symfony DIC.
README 文档
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.
统计信息
- 总下载量: 36.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-23