19/10/2014 - DOCTRINE, SYMFONY
Aşağıdaki örnekte, bir yandan User entitisine bilgi girerken, değişiklik yaparken ve bilgi silerken, diğer yandan da UserLog entitisine even listener ile bilgi giriyoruz.
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();
}
}
services:
application_backend.event_listener.user_entity:
class: Application\BackendBundle\EventListener\UserEntityListener
tags:
- { name: doctrine.event_listener, event: onFlush }
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);
}
}
}
}
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)