public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Order) {
if ($args->hasChangedField('status') && $args->getNewValue('status') == 'stock') {
$this->container->get('activity_logger')->writeLog($entity, 'purchase');
}
}
public function writeLog ($object, $comment)
{
$entity = new Stock();
$entity->setCategory($object->getIsotope()->getCategory()->getId());
$entity->setComment($comment);
$entity->setDate(new \DateTime('now'));
$entity->setUser($object->getUser()->getId());
$entity->setChange(TRUE);
$this->em->persist($entity);
$this->em->flush();
}
class PurchaseListener {
protected $notifications = array();
public function preUpdate(LifecycleEventArgs $args) {
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof Purchase) {
...
$notification = new Notification();
$notification->setUser($entity->getUser())
->setText('блабла')
->setType('success');
$this->notifications[] = $notification;
...
}
}
public function postFlush(PostFlushEventArgs $event)
{
if(count($this->notifications)>0) {
$em = $event->getEntityManager();
foreach ($this->notifications as $thing) {
$em->persist($thing);
}
$this->notifications = array();
$em->flush();
}
}
}
$this->em->persist($entity);
$meta = $this->em->getClassMetadata(get_class($entity));
$this->em->getUnitOfWork()->computeChangeSet($meta, $entity);