File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed
tests/Gedmo/SoftDeleteable Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments