Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ a release.
---

## [Unreleased]
### Added
- SoftDeleteable: Add option to enable or disable handling of the `postFlush` event (#2958)

## [3.20.0] - 2025-04-04
### Fixed
Expand Down
30 changes: 29 additions & 1 deletion src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,25 @@ class SoftDeleteableListener extends MappedEventSubscriber
*/
public const POST_SOFT_DELETE = 'postSoftDelete';

/**
* Whether the postFlush event should be handled.
*/
private bool $handlePostFlushEvent;

/**
* Objects soft-deleted on flush.
*
* @var array<object>
*/
private array $softDeletedObjects = [];

public function __construct(bool $handlePostFlushEvent = false)
{
parent::__construct();

$this->handlePostFlushEvent = $handlePostFlushEvent;
}

/**
* @return string[]
*/
Expand Down Expand Up @@ -138,7 +150,9 @@ public function onFlush(EventArgs $args)
);
}

$this->softDeletedObjects[] = $object;
if ($this->handlePostFlushEvent) {
$this->softDeletedObjects[] = $object;
}
}
}
}
Expand All @@ -150,6 +164,10 @@ public function onFlush(EventArgs $args)
*/
public function postFlush(EventArgs $args)
{
if (!$this->handlePostFlushEvent) {
return;
}

$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
foreach ($this->softDeletedObjects as $index => $object) {
Expand All @@ -172,6 +190,16 @@ public function loadClassMetadata(EventArgs $eventArgs)
$this->loadMetadataForObjectClass($eventArgs->getObjectManager(), $eventArgs->getClassMetadata());
}

public function setHandlePostFlushEvent(bool $handlePostFlushEvent): void
{
$this->handlePostFlushEvent = $handlePostFlushEvent;
}

public function shouldHandlePostFlushEvent(): bool
{
return $this->handlePostFlushEvent;
}

protected function getNamespace()
{
return __NAMESPACE__;
Expand Down
16 changes: 16 additions & 0 deletions tests/Gedmo/SoftDeleteable/Fixture/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class Article
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'article', cascade: ['persist', 'remove'])]
private $comments;

/**
* @ORM\ManyToOne(targetEntity="Author", cascade={"persist"}, inversedBy="articles")
*/
#[ORM\ManyToOne(targetEntity: Author::class, cascade: ['persist'], inversedBy: 'articles')]
private ?Author $author = null;

public function __construct()
{
$this->comments = new ArrayCollection();
Expand Down Expand Up @@ -100,4 +106,14 @@ public function getComments(): Collection
{
return $this->comments;
}

public function setAuthor(?Author $author): void
{
$this->author = $author;
}

public function getAuthor(): ?Author
{
return $this->author;
}
}
106 changes: 106 additions & 0 deletions tests/Gedmo/SoftDeleteable/Fixture/Entity/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\SoftDeleteable\Fixture\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;

/**
* @ORM\Entity
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
#[ORM\Entity]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class Author
{
use SoftDeleteableEntity;

/**
* @var int|null
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: Types::STRING)]
private ?string $firstname = null;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: Types::STRING)]
private ?string $lastname = null;

/**
* @var Collection<int, Article>
*
* @ORM\OneToMany(targetEntity="Article", mappedBy="author", cascade={"persist"})
*/
#[ORM\OneToMany(targetEntity: Article::class, mappedBy: 'author', cascade: ['persist'])]
private Collection $articles;

public function __construct()
{
$this->articles = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function setFirstname(?string $firstname): void
{
$this->firstname = $firstname;
}

public function getFirstname(): ?string
{
return $this->firstname;
}

public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}

public function getLastname(): ?string
{
return $this->lastname;
}

public function addArticle(Article $article): void
{
$this->articles[] = $article;
}

/**
* @return Collection<int, Article>
*/
public function getArticles(): Collection
{
return $this->articles;
}
}
44 changes: 43 additions & 1 deletion tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\Tests\Clock;
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Article;
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Author;
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Child;
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Comment;
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\MegaPage;
Expand Down Expand Up @@ -522,8 +523,10 @@ public function testShouldFilterBeQueryCachedCorrectlyWhenToggledForEntity(): vo
static::assertCount(0, $data);
}

public function testSoftDeletedObjectIsRemovedPostFlush(): void
public function testSoftDeletedObjectIsRemovedPostFlushWhenEnabled(): void
{
$this->softDeleteableListener->setHandlePostFlushEvent(true);

$repo = $this->em->getRepository(Article::class);
$commentRepo = $this->em->getRepository(Comment::class);

Expand Down Expand Up @@ -558,6 +561,44 @@ public function testSoftDeletedObjectIsRemovedPostFlush(): void
static::assertNull($commentRepo->find($comment->getId()));
}

public function testSoftDeletedEntityIsNotReinsertedPostFlushWhenDisabled(): void
{
$authorRepo = $this->em->getRepository(Author::class);

$author = new Author();
$firstname = 'first_name';
$author->setFirstname($firstname);
$lastname = 'last_name';
$author->setLastname($lastname);

$article = new Article();
$title = 'Title 1';
$article->setTitle($title);
$article->setAuthor($author);

$this->em->persist($article);
$this->em->flush();

$this->em->clear();

$author = $authorRepo->findOneBy(['firstname' => $firstname]);
$article = $author->getArticles()[0];

static::assertSame($lastname, $author->getLastname());
static::assertNull($author->getDeletedAt());
static::assertSame($title, $article->getTitle());

$this->em->remove($author);
$this->em->flush();

// Flush again
$this->em->flush();

// Check whether the entity was re-inserted
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
static::assertSame(1, $authorRepo->count(['firstname' => $firstname]));
}

public function testPostSoftDeleteEventIsDispatched(): void
{
$this->em->getEventManager()->addEventSubscriber(new WithPreAndPostSoftDeleteEventArgsTypeListener());
Expand Down Expand Up @@ -614,6 +655,7 @@ protected function getUsedEntityFixtures(): array
{
return [
Article::class,
Author::class,
Page::class,
MegaPage::class,
Module::class,
Expand Down