Open
Description
Bug Report
Q | A |
---|---|
BC Break | yes |
Version | 2.17.1 |
Summary
When updating orm with version 2.14.1
to 2.17.1
, some events stopped working. These are quite old tests and they still worked.
The idea was that after deleting an entity, check if there are more related entities, if no, then delete the parent(owner).
Current behavior
Parent(owner) entity stopped being deleted.
How to reproduce
namespace App\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use App\Entity\PullRequest\Comment;
final class NoteAutoRemoveListener implements EventSubscriber
{
public function getSubscribedEvents(): array
{
return [
Events::postRemove,
];
}
public function postRemove(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
$entityManager = $args->getObjectManager();
if ($entity instanceof Comment) {
$this->removeDiscussionNote($entity, $entityManager);
}
}
private function removeDiscussionNote(Comment $comment, EntityManagerInterface $entityManager): void
{
$discussion = $comment->getNote();
if (null !== $discussion && 0 === $discussion->getComments()->count()) {
$entityManager->remove($discussion);
}
}
}
and test
namespace App\Tests\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\PullRequest\Comment;
use App\Entity\PullRequest\DiscussionNote;
use App\Entity\PullRequest\Note;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
final class NoteAutoRemoveListenerTest extends KernelTestCase
{
private EntityManagerInterface $em;
protected function setUp(): void
{
parent::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
}
public function testRemoveDiscussionNoteAfterRemoveComment(): void
{
$pullRequest = $this->createPullRequest(); // method create entity
$this->em->persist($pullRequest);
$this->em->flush();
$comment = (new Comment())
->setBody('text')
->setUser($this->em->getRepository(User::class)->findOneBy([]))
->setPullRequest($pullRequest);
$note = (new DiscussionNote())
->setUser($comment->getUser())
->setPullRequest($pullRequest)
->addComment($comment);
$this->em->persist($note);
$this->em->flush();
$this->em->refresh($note);
$note = $this->em->getRepository(Note::class)->find($note->getId());
$this->assertSame(1, $note->getComments()->count());
$this->assertNotNull($note->getId());
$this->em->remove($comment);
$this->em->flush();
$this->assertNull($note->getId());
}
}