Récupération des valeurs d'origine des propriétés d'une entité Doctrine après une mise à jour

Publié le 25/01/2022 • Actualisé le 25/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 récupérer des valeurs d'origine des propriétés d'une entité Doctrine après une mise à jour. On apprend tous les jours, j'utilise Doctrine depuis des années, mais j'ai découvert cette fonction assez récemment (on peut aussi détecter des modifications avec les événements et ensembles de changements Doctrine). Cette fonction ne retourne pas une entité mais un tableau associatif champ / valeur.


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

/**
 * 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.
 */
trait Snippet186Trait
{
    public function snippet186(): void
    {
        $snippet = $this->articleData->getSnippetById(186);
        $originalAuthor = $snippet->getAuthor();
        $snippet->setAuthor('Les Tilleuls');
        $originalEntityData = $this->entityManager->getUnitOfWork()->getOriginalEntityData($snippet);

        echo 'Original author: '.$originalAuthor.PHP_EOL;
        echo 'New author: '.$snippet->getAuthor().PHP_EOL;
        echo 'Original author using the getOriginalEntityData() function: '.$originalEntityData['author'].PHP_EOL;

        // 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  Plus sur le web

  Travaillez avec moi !

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Controller\Snippets;

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

/**
 * @see Snippet186Trait
 */
final class Snippet186Test 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 Snippet186Trait::snippet186
     */
    public function testSnippet186(): void
    {
        $snippet = $this->articleData->getSnippetById(186);
        $originalAuthor = $snippet->getAuthor();
        $snippet->setAuthor('Les Tilleuls');
        $originalEntityData = $this->doctrine->getUnitOfWork()->getOriginalEntityData($snippet);

        self::assertNotSame($originalAuthor, $snippet->getAuthor());
        self::assertSame($originalAuthor, $originalEntityData['author']);
    }
}