Created
February 8, 2019 13:44
-
-
Save ger86/24d88309fd224c511df474ab0b1a1c14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Doctrine\EntityListener; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use Doctrine\ORM\Event\OnFlushEventArgs; | |
| use App\Entity\Post; | |
| class PostEntityListener { | |
| /** | |
| * @inheritDoc | |
| * @param OnFlushEventArgs $event | |
| * @return void | |
| */ | |
| public function onFlush(OnFlushEventArgs $event) { | |
| $em = $event->getEntityManager(); | |
| $uow = $em->getUnitOfWork(); | |
| foreach ($uow->getScheduledCollectionUpdates() as $collection) { | |
| $entity = $collection->getOwner(); | |
| if ($entity instanceof Post) { | |
| foreach ($collection->getDeleteDiff() as $itemDeleted) { | |
| if ($itemDeleted instanceof Comment) { | |
| $historyRecord = new HistoryRecord($entity, $itemDeleted, 'remove'); | |
| $em->persist($historyRecord); | |
| $uow->computeChangeSet($em->getClassMetadata(get_class($historyRecord)), $historyRecord); | |
| } | |
| } | |
| foreach ($collection->getInsertDiff() as $itemInserted) { | |
| if ($itemInserted instanceof Comment) { | |
| $historyRecord = new HistoryRecord($entity, $itemInserted, 'inserted'); | |
| $em->persist($historyRecord); | |
| $uow->computeChangeSet($em->getClassMetadata(get_class($historyRecord)), $historyRecord); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment