Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

In example below, while we're inserting, updating and removing a record to/from User entity, we're inserting records into UserLog entity with event listener.


UserController.php


public function create()
{
$user = new User();
$user->setUsername(substr(str_shuffle('ABCDEFGHIJKLMNOPRSTUVYZ'), 0, 6));
$user->setPassword(time());
$this->entityManager->persist($user);
$this->entityManager->flush();
}

public function update($username)
{
$user = $this->userRepository->findOneByUsername($username);
if ($user instanceof User) {
$user->setPassword(time());
$this->entityManager->flush();
}
}

public function remove($username)
{
$user = $this->userRepository->findOneByUsername($username);
if ($user instanceof User) {
$this->entityManager->remove($user);
$this->entityManager->flush();
}
}

Listeners.yml


services:
application_backend.event_listener.user_entity:
class: Application\BackendBundle\EventListener\UserEntityListener
tags:
- { name: doctrine.event_listener, event: onFlush }

UserEntityListener.php


namespace Application\BackendBundle\EventListener;

use Application\BackendBundle\Entity\User;
use Application\BackendBundle\Entity\UserLog;
use Doctrine\ORM\Event\OnFlushEventArgs;

class UserEntityListener
{
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setUserId($entity->getId());
$userLog->setMessage('onFlush - insert at ' . date('d/m/Y H:i:s'));

$em->persist($userLog);
// Instead of $em->flush() because we are already in flush process
$uow->computeChangeSet($em->getClassMetadata(get_class($userLog)), $userLog);
}
}

foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setUserId($entity->getId());
$userLog->setMessage('onFlush - update at ' . date('d/m/Y H:i:s'));

$em->persist($userLog);
// Instead of $em->flush() because we are already in flush process
$uow->computeChangeSet($em->getClassMetadata(get_class($userLog)), $userLog);
}
}

foreach ($uow->getScheduledEntityDeletions() as $entity) {
if ($entity instanceof User) {
$userLog = new UserLog();
$userLog->setUserId($entity->getId());
$userLog->setMessage('onFlush - delete at ' . date('d/m/Y H:i:s'));

$em->persist($userLog);
// Instead of $em->flush() because we are already in flush process
$uow->computeChangeSet($em->getClassMetadata(get_class($userLog)), $userLog);
}
}
}
}

Result


mysql> SELECT id, username, password FROM user;
+----+----------+------------+
| id | username | password |
+----+----------+------------+
| 1 | YFJLPO | 1435779900 |
+----+----------+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM user_log;
+----+---------+-----------------------------------------+
| id | user_id | message |
+----+---------+-----------------------------------------+
| 1 | NULL | onFlush - insert at 01/07/2015 20:45:00 |
+----+---------+-----------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT id, username, password FROM user;
+----+----------+------------+
| id | username | password |
+----+----------+------------+
| 1 | YFJLPO | 1435779967 |
+----+----------+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM user_log;
+----+---------+-----------------------------------------+
| id | user_id | message |
+----+---------+-----------------------------------------+
| 1 | NULL | onFlush - insert at 01/07/2015 20:45:00 |
| 2 | 1 | onFlush - update at 01/07/2015 20:46:07 |
+----+---------+-----------------------------------------+
2 rows in set (0.00 sec)

mysql> SELECT id, username, password FROM user;
Empty set (0.00 sec)

mysql> SELECT * FROM user_log;
+----+---------+-----------------------------------------+
| id | user_id | message |
+----+---------+-----------------------------------------+
| 1 | NULL | onFlush - insert at 01/07/2015 20:45:00 |
| 2 | 1 | onFlush - update at 01/07/2015 20:46:07 |
| 3 | 1 | onFlush - delete at 01/07/2015 20:47:21 |
+----+---------+-----------------------------------------+
3 rows in set (0.00 sec)