@@ -290,6 +290,162 @@ public function testSoftDeleteable()
290290 $ this ->assertInstanceOf (self ::OTHER_COMMENT_CLASS , $ foundComment );
291291 }
292292
293+ /**
294+ * @group datetimeinterface
295+ */
296+ public function testSoftDeleteableWithDateTimeInterface ()
297+ {
298+ $ repo = $ this ->em ->getRepository (self ::ARTICLE_CLASS );
299+ $ commentRepo = $ this ->em ->getRepository (self ::COMMENT_CLASS );
300+
301+ $ comment = new Comment ();
302+ $ commentField = 'comment ' ;
303+ $ commentValue = 'Comment 1 ' ;
304+ $ comment ->setComment ($ commentValue );
305+ $ art0 = new Article ();
306+ $ field = 'title ' ;
307+ $ value = 'Title 1 ' ;
308+ $ art0 ->setTitle ($ value );
309+ $ art0 ->addComment ($ comment );
310+
311+ $ this ->em ->persist ($ art0 );
312+ $ this ->em ->flush ();
313+
314+ $ art = $ repo ->findOneBy (array ($ field => $ value ));
315+
316+ $ this ->assertNull ($ art ->getDeletedAt ());
317+ $ this ->assertNull ($ comment ->getDeletedAt ());
318+
319+ $ art ->setDeletedAt (new \DateTimeImmutable ());
320+ $ this ->em ->flush ();
321+
322+ $ art = $ repo ->findOneBy (array ($ field => $ value ));
323+ $ this ->assertNull ($ art );
324+
325+ // Now we deactivate the filter so we test if the entity appears in the result
326+ $ this ->em ->getFilters ()->disable (self ::SOFT_DELETEABLE_FILTER_NAME );
327+
328+ $ art = $ repo ->findOneBy (array ($ field => $ value ));
329+ $ this ->assertInternalType ('object ' , $ art );
330+ $ this ->assertInternalType ('object ' , $ art ->getDeletedAt ());
331+ $ this ->assertInstanceOf ('DateTimeInterface ' , $ art ->getDeletedAt ());
332+ $ comment = $ commentRepo ->findOneBy (array ($ commentField => $ commentValue ));
333+ $ this ->assertInternalType ('object ' , $ comment );
334+ $ this ->assertNull ($ comment ->getDeletedAt ());
335+
336+ $ this ->em ->createQuery ('UPDATE ' .self ::ARTICLE_CLASS .' a SET a.deletedAt = NULL ' )->execute ();
337+
338+ $ this ->em ->refresh ($ art );
339+ $ this ->em ->refresh ($ comment );
340+
341+ // Now we try with a DQL Delete query
342+ $ this ->em ->getFilters ()->enable (self ::SOFT_DELETEABLE_FILTER_NAME );
343+ $ dql = sprintf ('DELETE FROM %s a WHERE a.%s = :%s ' ,
344+ self ::ARTICLE_CLASS , $ field , $ field );
345+ $ query = $ this ->em ->createQuery ($ dql );
346+ $ query ->setParameter ($ field , $ value );
347+ $ query ->setHint (
348+ \Doctrine \ORM \Query::HINT_CUSTOM_OUTPUT_WALKER ,
349+ 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker '
350+ );
351+
352+ $ query ->execute ();
353+
354+ $ art = $ repo ->findOneBy (array ($ field => $ value ));
355+ $ this ->assertNull ($ art );
356+
357+ // Now we deactivate the filter so we test if the entity appears in the result
358+ $ this ->em ->getFilters ()->disable (self ::SOFT_DELETEABLE_FILTER_NAME );
359+ $ this ->em ->clear ();
360+
361+ $ art = $ repo ->findOneBy (array ($ field => $ value ));
362+
363+ $ this ->assertInternalType ('object ' , $ art );
364+ $ this ->assertInternalType ('object ' , $ art ->getDeletedAt ());
365+ $ this ->assertInstanceOf ('DateTimeInterface ' , $ art ->getDeletedAt ());
366+
367+ // Inheritance tree DELETE DQL
368+ $ this ->em ->getFilters ()->enable (self ::SOFT_DELETEABLE_FILTER_NAME );
369+
370+ $ megaPageRepo = $ this ->em ->getRepository (self ::MEGA_PAGE_CLASS );
371+ $ module = new Module ();
372+ $ module ->setTitle ('Module 1 ' );
373+ $ page = new MegaPage ();
374+ $ page ->setTitle ('Page 1 ' );
375+ $ page ->addModule ($ module );
376+ $ module ->setPage ($ page );
377+
378+ $ this ->em ->persist ($ page );
379+ $ this ->em ->persist ($ module );
380+ $ this ->em ->flush ();
381+
382+ $ dql = sprintf ('DELETE FROM %s p ' ,
383+ self ::PAGE_CLASS );
384+ $ query = $ this ->em ->createQuery ($ dql );
385+ $ query ->setHint (
386+ \Doctrine \ORM \Query::HINT_CUSTOM_OUTPUT_WALKER ,
387+ 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker '
388+ );
389+
390+ $ query ->execute ();
391+
392+ $ p = $ megaPageRepo ->findOneBy (array ('title ' => 'Page 1 ' ));
393+ $ this ->assertNull ($ p );
394+
395+ // Now we deactivate the filter so we test if the entity appears in the result
396+ $ this ->em ->getFilters ()->disable (self ::SOFT_DELETEABLE_FILTER_NAME );
397+ $ this ->em ->clear ();
398+
399+ $ p = $ megaPageRepo ->findOneBy (array ('title ' => 'Page 1 ' ));
400+
401+ $ this ->assertInternalType ('object ' , $ p );
402+ $ this ->assertInternalType ('object ' , $ p ->getDeletedAt ());
403+ $ this ->assertInstanceOf ('DateTimeInterface ' , $ p ->getDeletedAt ());
404+
405+ // Test of #301
406+ $ this ->em ->getFilters ()->enable (self ::SOFT_DELETEABLE_FILTER_NAME );
407+
408+ $ otherArticleRepo = $ this ->em ->getRepository (self ::OTHER_ARTICLE_CLASS );
409+ $ otherCommentRepo = $ this ->em ->getRepository (self ::OTHER_COMMENT_CLASS );
410+ $ otherArt = new OtherArticle ();
411+ $ otherComment = new OtherComment ();
412+ $ otherArt ->setTitle ('Page 1 ' );
413+ $ otherComment ->setComment ('Comment ' );
414+ $ otherArt ->addComment ($ otherComment );
415+ $ otherComment ->setArticle ($ otherArt );
416+
417+ $ this ->em ->persist ($ otherArt );
418+ $ this ->em ->persist ($ otherComment );
419+ $ this ->em ->flush ();
420+
421+ $ this ->em ->refresh ($ otherArt );
422+ $ this ->em ->refresh ($ otherComment );
423+
424+ $ artId = $ otherArt ->getId ();
425+ $ commentId = $ otherComment ->getId ();
426+
427+ $ otherArt ->setDeletedAt (new \DateTimeImmutable ());
428+ $ this ->em ->flush ();
429+
430+ $ foundArt = $ otherArticleRepo ->findOneBy (array ('id ' => $ artId ));
431+ $ foundComment = $ otherCommentRepo ->findOneBy (array ('id ' => $ commentId ));
432+
433+ $ this ->assertNull ($ foundArt );
434+ $ this ->assertInternalType ('object ' , $ foundComment );
435+ $ this ->assertInstanceOf (self ::OTHER_COMMENT_CLASS , $ foundComment );
436+
437+ $ this ->em ->getFilters ()->disable (self ::SOFT_DELETEABLE_FILTER_NAME );
438+
439+ $ foundArt = $ otherArticleRepo ->findOneById ($ artId );
440+ $ foundComment = $ otherCommentRepo ->findOneById ($ commentId );
441+
442+ $ this ->assertInternalType ('object ' , $ foundArt );
443+ $ this ->assertInternalType ('object ' , $ foundArt ->getDeletedAt ());
444+ $ this ->assertInstanceOf ('DateTimeInterface ' , $ foundArt ->getDeletedAt ());
445+ $ this ->assertInternalType ('object ' , $ foundComment );
446+ $ this ->assertInstanceOf (self ::OTHER_COMMENT_CLASS , $ foundComment );
447+ }
448+
293449 /**
294450 * Make sure that soft delete also works when configured on a mapped superclass
295451 */
0 commit comments