I have a system where a user can create an order. As soon as the order is created another entity table should be updated with the current user who created the order, the time he created it, and some other information.
Basically a Log.
Here is my Listener class;
namespace Qi\Bss\BaseBundle\Lib\PurchaseModule;
use Qi\Bss\BaseBundle\Entity\Business\PurchaseModule\PmodLog;
use Qi\Bss\BaseBundle\Entity\Business\PurchaseModule\PmodOrder;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* Class OrderLogger. Purchase Module activity logger.
*/
class OrderLogger
{
/**
* Service container
* @var type
*/
private $serviceContainer;
/**
* Performs tasks before destruction
* @ORM\PostPersist
*/
public function postPersist(LifecycleEventArgs $args)
{
$order = $args->getEntity();
if ($order instanceof PmodOrder) {
$logRecord = new PmodLog();
$user = $this->serviceContainer->get('security.token_storage')->getToken()->getUser();
$logRecord->setCreatedBy($user);
$logRecord->setAction('Gemaak');
$logRecord->setCreatedAt(new \DateTime());
$logRecord->setOrder($order);
$logRecord->setDepartment($user->getDepartment());
}
}
/**
* Sets the sales order exporter object
* @param type $serviceContainer
*/
public function setServiceContainer($serviceContainer)
{
$this->serviceContainer = $serviceContainer;
}
}
I don't get what I'm doing wrong and that the log table in my database doesn't get updated whatsoever.
Here is my service as well;
bss.pmod.logger:
class: Qi\Bss\BaseBundle\Lib\PurchaseModule\OrderLogger
calls:
- [ setServiceContainer, [@service_container] ]
tags:
- { name: doctrine.event_listener, event: postPersist }
I don't get an error, the order is perfectly created but the log database remain empty.