Remove postPersist. #330
This commit is contained in:
parent
c748a19da8
commit
f46c56c41e
3 changed files with 23 additions and 179 deletions
|
|
@ -261,10 +261,9 @@ services:
|
|||
|
||||
App\EventListener\EntityListener\UserEntityListener:
|
||||
arguments:
|
||||
["@security.token_storage"]
|
||||
$token_storage: "@security.token_storage"
|
||||
$log_em: "@doctrine.orm.logging_entity_manager"
|
||||
tags:
|
||||
- name: 'doctrine.orm.entity_listener'
|
||||
event: 'postPersist'
|
||||
entity: 'App\Entity\User'
|
||||
- name: 'doctrine.event_listener'
|
||||
event: 'onFlush'
|
||||
entity: 'App\Entity\User'
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\EventListener;
|
|||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||
|
||||
|
|
@ -14,40 +15,16 @@ use DateTime;
|
|||
abstract class EntityListener
|
||||
{
|
||||
protected $token_storage;
|
||||
protected $log_em;
|
||||
|
||||
public function __construct(TokenStorage $token_storage)
|
||||
public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em)
|
||||
{
|
||||
$this->token_storage = $token_storage;
|
||||
}
|
||||
|
||||
public function postPersist($object, LifecycleEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
$event_time = new DateTime();
|
||||
|
||||
// get user for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
$username = $user->getUsername();
|
||||
|
||||
$entity_log = new EntityLog();
|
||||
$entity_log->setUser($username)
|
||||
->setDateAction($event_time)
|
||||
->setAction('Create');
|
||||
|
||||
$log_message = 'Object with id ' . $object->getID() . ' created.';
|
||||
$entity_log->addChange('Object', $log_message);
|
||||
|
||||
$em->persist($entity_log);
|
||||
$em->flush();
|
||||
$this->log_em = $log_em;
|
||||
}
|
||||
|
||||
public function onFlush(OnFlushEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
// get date time of event for logging
|
||||
|
|
@ -64,11 +41,21 @@ abstract class EntityListener
|
|||
$field_changes = [];
|
||||
|
||||
$unit_of_work = $em->getUnitOfWork();
|
||||
$created_entities = $unit_of_work->getScheduledEntityInsertions();
|
||||
$deleted_entities = $unit_of_work->getScheduledEntityDeletions();
|
||||
$updated_entities = $unit_of_work->getScheduledEntityUpdates();
|
||||
$updated_collections = $unit_of_work->getScheduledCollectionUpdates();
|
||||
$deleted_collections = $unit_of_work->getScheduledCollectionDeletions();
|
||||
|
||||
// get objects for creation
|
||||
foreach ($created_entities as $created_entity)
|
||||
{
|
||||
$log_message = 'Object with id ' . $created_entity->getID() . ' created.';
|
||||
|
||||
$entity_log->setAction('Create')
|
||||
->addChange('Object', $log_message);
|
||||
}
|
||||
|
||||
// get updated fields. This doesn't include lists of other entities.
|
||||
foreach ($updated_entities as $updated_entity)
|
||||
{
|
||||
|
|
@ -105,7 +92,7 @@ abstract class EntityListener
|
|||
->addChange('Object', $log_message);
|
||||
}
|
||||
|
||||
//$em->persist($entity_log);
|
||||
$this->log_em->persist($entity_log);
|
||||
|
||||
// TODO: find a way to get the list of "old" items, compare with list of updated items
|
||||
// so we can find out what was added/removed
|
||||
|
|
|
|||
|
|
@ -4,163 +4,21 @@ namespace App\EventListener\EntityListener;
|
|||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
|
||||
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\EventListener\EntityListener;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Entity\Role;
|
||||
use App\Entity\Hub;
|
||||
|
||||
use App\Entity\Logging\EntityLog;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class UserEntityListener extends EntityListener
|
||||
{
|
||||
protected $token_storage;
|
||||
protected $log_em;
|
||||
|
||||
public function __construct(TokenStorage $token_storage)
|
||||
public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em)
|
||||
{
|
||||
$this->token_storage = $token_storage;
|
||||
$this->log_em = $log_em;
|
||||
|
||||
parent::__construct($token_storage);
|
||||
parent::__construct($token_storage, $log_em);
|
||||
}
|
||||
|
||||
/*
|
||||
public function postPersist($user, LifecycleEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
$event_time = new DateTime();
|
||||
$log_time = $event_time->format('Y-m-d H:i:s');
|
||||
|
||||
// get user for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
$username = $user->getUsername();
|
||||
|
||||
$entity_log = new EntityLog();
|
||||
$entity_log->setUser($username)
|
||||
->setDateAction($event_time)
|
||||
->setAction('Create');
|
||||
|
||||
$entity_log->addChange('User', 'User created.');
|
||||
|
||||
$em->persist($entity_log);
|
||||
$em->flush();
|
||||
|
||||
}
|
||||
|
||||
//public function onFlush(OnFlushEventArgs $args)
|
||||
//{
|
||||
/*
|
||||
$em = $args->getEntityManager();
|
||||
$unit_of_work = $em->getUnitOfWork();
|
||||
$deleted_entities = $unit_of_work->getScheduledEntityDeletions();
|
||||
$updated_entities = $unit_of_work->getScheduledEntityUpdates();
|
||||
$updated_collections = $unit_of_work->getScheduledCollectionUpdates();
|
||||
$deleted_collections = $unit_of_work->getScheduledCollectionDeletions();
|
||||
|
||||
foreach ($updated_entities as $updated_entity)
|
||||
{
|
||||
//if ($updated_entity instanceof User)
|
||||
//{
|
||||
$changeset = $unit_of_work->getEntityChangeSet($updated_entity);
|
||||
|
||||
$entity_fields = array_keys($changeset);
|
||||
foreach ($entity_fields as $field)
|
||||
{
|
||||
$values = $changeset[$field];
|
||||
|
||||
$old_value = $values[0];
|
||||
$new_value = $values[1];
|
||||
|
||||
error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
foreach ($deleted_entities as $deleted_entity)
|
||||
{
|
||||
//if ($deleted_entity instanceof User)
|
||||
//{
|
||||
$deleted_id = $deleted_entity->getID();
|
||||
$deleted_username = $deleted_entity->getUsername();
|
||||
|
||||
error_log('Deleted user with id ' . $deleted_id . ' and username ' . $deleted_username);
|
||||
//}
|
||||
}
|
||||
|
||||
// TODO: find a way to get the list of "old" items, compare with list of updated items
|
||||
// so we can find out what was added/removed
|
||||
foreach ($updated_collections as $updated_collection)
|
||||
{
|
||||
error_log('in updated_collection snippet');
|
||||
|
||||
foreach ($updated_collection as $item)
|
||||
{
|
||||
if ($item instanceof Role)
|
||||
{
|
||||
error_log('updated_collection item ' . $item->getName());
|
||||
}
|
||||
if ($item instanceof Hub)
|
||||
{
|
||||
error_log('updated_collection item ' . $item->getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach ($deleted_collections as $deleted_collection)
|
||||
{
|
||||
error_log('in deleted_collection snippet');
|
||||
|
||||
foreach ($deleted_collection as $item)
|
||||
{
|
||||
if ($item instanceof Role)
|
||||
{
|
||||
error_log('deleted_collection item ' . $item->getName());
|
||||
}
|
||||
if ($item instanceof Hub)
|
||||
{
|
||||
error_log('deleted_collection item ' . $item->getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} */
|
||||
|
||||
//}
|
||||
|
||||
/*
|
||||
public function postUpdate($user, LifecycleEventArgs $args)
|
||||
{
|
||||
error_log ('POST UPDATE');
|
||||
|
||||
error_log('User with id ' . $user->getID() . ' updated.');
|
||||
error_log('username ' . $user->getUsername());
|
||||
error_log('password ' . $user->getPassword());
|
||||
error_log('first name ' . $user->getFirstName());
|
||||
error_log('last name ' . $user->getLastName());
|
||||
error_log('contact number '. $user->getContactNumber());
|
||||
error_log('email ' . $user->getEmail());
|
||||
|
||||
$roles = $user->getRoles();
|
||||
$hubs = $user->getHubs();
|
||||
|
||||
foreach ($roles as $role)
|
||||
error_log('user role ' . $role);
|
||||
|
||||
foreach ($hubs as $hub)
|
||||
error_log('user hub ' . $hub);
|
||||
} */
|
||||
|
||||
/*
|
||||
public function postRemove($user, LifecycleEventArgs $args)
|
||||
{
|
||||
error_log('User deleted.');
|
||||
} */
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue