Last active
February 8, 2019 13:45
-
-
Save ger86/4fe210e8ccf8fbf81169414255904c96 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); | |
| } | |
| } | |
| foreach ($collection->getInsertDiff() as $itemInserted) { | |
| if ($itemInserted instanceof Comment) { | |
| $historyRecord = new HistoryRecord($entity, $itemInserted, 'inserted'); | |
| $em->persist($historyRecord); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment