Symfony event listener after save new entity -


i need check if entity instance of specific entity , after save new entity. method aftersave in cakephp. tried postflush, onflush, postpersist , postload.

appbundle/eventlistener/dosomethingaftersavenewentity.php

namespace appbundle\eventlistener;  use doctrine\orm\event\onflusheventargs; use appbundle\entity\someentity;  class dosomethingaftersavenewentity  {     public function onflush(onflusheventargs $args)     {            $entity = $args->getentity();         $em = $args->getentitymanager();         $uow = $em->getunitofwork();          if (!$entity instanceof someentity) {           die('not instance!!!');         }          die('post flush!!!');         $entitymanager = $args->getentitymanager();     } } 

your idea right, should create new entitylistener or entitysubscriber that. not entity listens changes (bad practice).

services.yml:

your.entity.subscriber:       class: appbundle\subscriber\yourentitysubscriber       tags:           - { name: doctrine.event_subscriber, connection: default } 

yourentitysubscriber.php:

<?php  namespace appbundle\subscriber;  use appbundle\entity\yourentity; use doctrine\common\eventsubscriber; use doctrine\orm\event\lifecycleeventargs;  class yourentitysubscriber implements eventsubscriber {     /**      * @return array      */     public function getsubscribedevents()     {         return array(             'postpersist',             'postupdate',         );     }      /**      * @param lifecycleeventargs $args      */     public function postpersist(lifecycleeventargs $args)     {         // whatever want (after entity creation)     }      /**      * @param lifecycleeventargs $args      */     public function postupdate(lifecycleeventargs $args)     {         // whatever want     } } 

also can more examples here: event_listeners_subscribers


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -