Skip to content

Commit 9ee1c35

Browse files
authored
Merge pull request #1868 from eoneopay/remove-soft-deleted-objects-from-cache
[SoftDeleteable] Detach soft-deleted objects from entity manager after flush
2 parents 9cde363 + 5af7477 commit 9ee1c35

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/Gedmo/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class SoftDeleteableListener extends MappedEventSubscriber
2929
*/
3030
const POST_SOFT_DELETE = "postSoftDelete";
3131

32+
/**
33+
* Objects soft-deleted on flush.
34+
*
35+
* @var array
36+
*/
37+
private $softDeletedObjects = array();
38+
3239
/**
3340
* {@inheritdoc}
3441
*/
@@ -37,6 +44,7 @@ public function getSubscribedEvents()
3744
return array(
3845
'loadClassMetadata',
3946
'onFlush',
47+
'postFlush'
4048
);
4149
}
4250

@@ -90,10 +98,32 @@ public function onFlush(EventArgs $args)
9098
self::POST_SOFT_DELETE,
9199
$ea->createLifecycleEventArgsInstance($object, $om)
92100
);
101+
102+
$this->softDeletedObjects[] = $object;
93103
}
94104
}
95105
}
96106

107+
/**
108+
* Detach soft-deleted objects from object manager.
109+
*
110+
* @param \Doctrine\Common\EventArgs $args
111+
*
112+
* @return void
113+
*
114+
* @throws \Gedmo\Exception\InvalidArgumentException
115+
*/
116+
public function postFlush(EventArgs $args)
117+
{
118+
$ea = $this->getEventAdapter($args);
119+
$om = $ea->getObjectManager();
120+
121+
foreach ($this->softDeletedObjects as $index => $object) {
122+
$om->detach($object);
123+
unset($this->softDeletedObjects[$index]);
124+
}
125+
}
126+
97127
/**
98128
* Maps additional metadata
99129
*

tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ public function shouldBeAbleToHardDeleteSoftdeletedItems()
7676
$this->assertNull($user);
7777
}
7878

79+
/**
80+
* @test
81+
*/
82+
public function shouldNotFetchSoftDeletedItemByIdIfDetachOnDeleteEnabled()
83+
{
84+
$repo = $this->em->getRepository(self::USER_CLASS);
85+
86+
$newUser = new User();
87+
$newUser->setUsername('test_user');
88+
89+
$this->em->persist($newUser);
90+
$this->em->flush();
91+
92+
$userId = $newUser->getId();
93+
94+
$this->em->remove($newUser);
95+
$this->em->flush();
96+
97+
$user = $repo->find($userId);
98+
$this->assertNull($user);
99+
}
100+
79101
/**
80102
* @test
81103
*/
@@ -420,7 +442,7 @@ protected function getUsedEntityFixtures()
420442
self::OTHER_ARTICLE_CLASS,
421443
self::OTHER_COMMENT_CLASS,
422444
self::MAPPED_SUPERCLASS_CHILD_CLASS,
423-
self::USER_NO_HARD_DELETE_CLASS,
445+
self::USER_NO_HARD_DELETE_CLASS
424446
);
425447
}
426448
}

0 commit comments

Comments
 (0)