calculator = $calculators; } /** * @return array */ public function getSubscribedEvents() { return [ Events::onFlush, ]; } /** * @param OnFlushEventArgs $args */ public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); $meta = $em->getClassMetadata(Timesheet::class); foreach ($uow->getScheduledEntityUpdates() as $entity) { if (!($entity instanceof Timesheet)) { continue; } $this->calculateFields($entity); $uow->recomputeSingleEntityChangeSet($meta, $entity); } foreach ($uow->getScheduledEntityInsertions() as $entity) { if (!($entity instanceof Timesheet)) { continue; } $this->calculateFields($entity); $uow->recomputeSingleEntityChangeSet($meta, $entity); } } /** * @param Timesheet $entity */ protected function calculateFields(Timesheet $entity) { foreach ($this->calculator as $calculator) { $calculator->calculate($entity); } } }