Tester si une entité Doctrine est planifiée pour la suppression

Publié le 12/01/2022 • Actualisé le 12/01/2022


English language detected! 🇬🇧

  We noticed that your browser is using English. Do you want to read this post in this language?

Read the english version 🇬🇧 Close

Dans ce bout de code, nous voyons comment tester si une entité Doctrine est planifiée pour la suppression. Il y a deux différentes manières de faire impliquant l'objet "unité de travail" (unit of work). On peut tester une entité particulière ou l'on peut récupérer une liste d'entités grâce à la fonction getScheduledEntityDeletions().


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

use App\Data\ArticleData;
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;

/**
 * J'utilise un trait PHP afin d'isoler chaque snippet dans un fichier.
 * Ce code doit être apellé d'un contrôleur Symfony étendant AbstractController (depuis Symfony 4.2)
 * ou Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Les services sont injectés dans le constructeur du contrôleur principal.
 *
 * @property ArticleData            $articleData
 * @property EntityManagerInterface $entityManager
 */
trait Snippet184Trait
{
    public function snippet184(): void
    {
        // Get the current snippet
        $snippet = $this->articleData->getSnippetById(184);

        // Remove it
        $this->entityManager->remove($snippet);

        $uow = $this->entityManager->getUnitOfWork();
        if ($uow->getEntityState($snippet) === UnitOfWork::STATE_REMOVED) {
            echo 'The snippet is scheduled for deletion ! ✅'.PHP_EOL;
        } else {
            echo 'The snippet is NOT scheduled for deletion. ❌'.PHP_EOL;
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            /** @var Article $entity */
            echo sprintf('The snippet "%s" is scheduled for deletion. ✅', $entity->getId());
        }

        // That's it! 😁
    }
}

 Exécuter le snippet  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow   Lire la doc  Snippet aléatoire

  Travaillez avec moi !

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Controller\Snippets;

use App\Data\ArticleData;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @see Snippet184Trait
 */
final class Snippet184Test extends KernelTestCase
{
    private EntityManagerInterface $doctrine;
    private ArticleData $articleData;

    protected function setUp(): void
    {
        $this->doctrine = self::getContainer()->get(EntityManagerInterface::class);
        $this->articleData = self::getContainer()->get(ArticleData::class);
    }

    /**
     * @see Snippet184Trait::snippet184
     */
    public function testSnippet184(): void
    {
        $uow = $this->doctrine->getUnitOfWork();
        $snippet = $this->articleData->getArticleById(183);
        $uow->remove($snippet);

        self::assertSame(UnitOfWork::STATE_REMOVED, $uow->getEntityState($snippet));
        self::assertCount(1, $uow->getScheduledEntityDeletions());
    }
}