php - Notification system in symfony 3.1 -
i use symfony 3.1. want create notification system in application in symfony. have created entity named notification save notifications. when user creates, edits or removes record in database, want save action in notification table. used haslifecyclecallbacks() annotation method , forced me create controller object in entity nothing has worked. how can it? there solution?
/** * @orm\table(name="user") * @orm\entity(repositoryclass="cm\userbundle\repository\userrepository") * @orm\haslifecyclecallbacks() */ class user extends baseuser { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @var string * * @orm\column(name="nom", type="string", unique=true, length=255, nullable=true) * @assert\notblank() */ protected $nom; /** * @var int * * @orm\column(name="numero", type="integer", unique=true, nullable=true) * @assert\notblank() */ protected $numero; /** * id * * @return int */ public function getid() { return $this->id; } /** * set nom * * @param string $nom * * @return user */ public function setnom($nom) { $this->nom = $nom; return $this; } /** * nom * * @return string */ public function getnom() { return $this->nom; } /** * set numero * * @param integer $numero * * @return user */ public function setnumero($numero) { $this->numero = $numero; return $this; } /** * numero * * @return int */ public function getnumero() { return $this->numero; } /** * @orm\preremove */ public function notify(){ $controlleur = new removecontroller(); $em = $controlleur->getdoctrine()->getmanager(); $notif = new notification(); $notif->setoperation('recording'); $notif->setuser('william'); $em->persist($notif); $em->flush(); } }
i've resolved problem. had not read documentation. , did more research. here solution works me in symfony 3.1. used dependency injection. have injected managerregistry have entity manager service , tokenstorage tokenstorage service know current user connected. have created folder name notificationdb. , have created class name notificationdb.php here code. example, suppose have form register foo entity.
namespace cm\gestionbundle\notificationbd; use doctrine\common\persistence\managerregistry; use symfony\component\security\core\authentication\token\storage\tokenstorage; use \cm\gestionbundle\entity\notification; class notificationbd { private $managerregistry; /** * @var tokenstorage */ private $tokenstorage; public function __construct(managerregistry $managerregistry, tokenstorage $tokenstorage) { $this->managerregistry = $managerregistry; $this->tokenstorage = $tokenstorage; } public function notifyevent(){ $entitymanager = $this->managerregistry->getmanager(); $user = $this->tokenstorage->gettoken()->getuser(); $notif = new notification(); $notif->setdatenotif(new \datetime()); $notif->setuser($user); $notif->setactionnotif('recording'); $notif->setobjetnotif('foo'); $entitymanager->persist($notifedition); $entitymanager->flush(); } }
in cm\gestionbundle\resources\config**, have configure service in **services.yml file here content :
cm_gestion.notification: class: cm\gestionbundle\notificationbd\notificationbd arguments: ['@doctrine', '@security.token_storage']
and have created listener call service. example, create foo entity listener listen register event , use service persist notification entity. here foo listener.
namespace cm\gestionbundle\doctrinelistener; use cm\gestionbundle\notificationbd\notificationbd; use doctrine\common\persistence\event\lifecycleeventargs; use \cm\gestionbundle\entity\foo; class foolistener { /** * @var notificationbd */ private $notificationbd; public function __construct(notificationbd $notificationbd) { $this->notificationbd = $notificationbd; } public function postpersist(lifecycleeventargs $args) { $entity = $args->getobject(); if (!$entity instanceof foo) { return; } $this->notificationbd->notifyevent(); } }
in cm\gestionbundle\resources\config last thing add listener in services.yml file. here content
cm_gestion.doctrine_listener.foo: class: cm\gestionbundle\doctrinelistener\foolistener arguments: - "@cm_gestion.notification" tags: - {name: doctrine.event_listener, event: postpersist}
that all. solution work me in symfony 3.1. problem can marked resolved. thank
Comments
Post a Comment