Reputation: 571
I use symfony 3.1. I want to create a notification system in my application in symfony. I have created an entity named notification to save notifications. So when a user creates, edits or removes a record in the database, I want to save this action in a notification table. I used HasLifecycleCallbacks() annotation method and it forced me to create a controller object in my entity but nothing has worked. How can i do it? Is there another 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;
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get 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();
}
}
Upvotes: 0
Views: 1778
Reputation: 571
I've resolved my problem. I had not read very well the documentation. And then i did some more research. So here is a solution that works for me in symfony 3.1. I used dependency injection. I have injected ManagerRegistry to have the entity manager service and TokenStorage for tokenStorage service to know the current user connected. I have created a folder name NotificationDB. And then i have also created a class name NotificationDB.php which here is the code. For this example, i suppose that i have a form to register a 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**, i have configure as service in **services.yml file which here is the content :
CM_gestion.notification:
class: CM\GestionBundle\NotificationBD\NotificationBD
arguments: ['@doctrine', '@security.token_storage']
And then i have created a listener for which will call this service. For this example, i will create a foo entity listener which will listen register event and use the service to persist the notification entity. Here is my 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 The last thing to do is to add this listener in services.yml file. Here is the content
CM_gestion.doctrine_listener.foo:
class: CM\GestionBundle\DoctrineListener\FooListener
arguments:
- "@CM_gestion.notification"
tags:
- {name: doctrine.event_listener, event: postPersist}
That all. this solution work for me in symfony 3.1. This problem can be marked as resolved. Thank
Upvotes: 1