Test if a Doctrine entity is already persisted in the database

Published on 2018-09-11 • Modified on 2018-09-23

This code is called from an action function of a Symfony 4.1 standard controller (extending Symfony\Bundle\FrameworkBundle\Controller\Controller). If you want to test if an entity is modified, check out this snippet.


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

use App\Entity\Article;
use App\Enum\ArticleType;
use Doctrine\ORM\EntityManagerInterface;

/**
 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
 *
 * @property EntityManagerInterface $entityManager
 */
trait Snippet2Trait
{
    public function snippet2(): void
    {
        $article = new Article(); // create a fresh Doctrine object
        $isPersisted = $this->entityManager->contains($article);
        echo $isPersisted ? 'true' : 'false'; // returns false

        // Set minimum database constraints so the entity can be persisted
        $article->setType(ArticleType::BLOG_POST);
        $article->setActive(false);
        // $this->manager->persist($article); // persist in database
        // (I will not persist because I don't want to pollute my database
        // but you've got the spirit!)
        $this->entityManager->flush();

        $isPersisted = $this->entityManager->contains($article);
        echo PHP_EOL;
        echo $isPersisted ? 'true' : 'false'; // would returns true without the line commented.

        // That's it! 😁
    }
}

 Run this snippet  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  More on Stackoverflow  Random snippet

  Work with me!

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Controller\Snippets;

use App\Entity\Article;
use App\Enum\ArticleType;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @see Snippet2Trait
 */
final class Snippet2Test extends KernelTestCase
{
    private Registry $doctrine;

    protected function setUp(): void
    {
        /** @var Registry $doctrine */
        $doctrine = self::getContainer()->get('doctrine');
        $this->doctrine = $doctrine;
    }

    /**
     * @see Snippet2Trait::snippet2
     */
    public function testSnippet2(): void
    {
        $projectDir = self::getContainer()->getParameter('kernel.project_dir');
        self::assertFileExists($projectDir.'/src/Controller/Snippet/Snippet2Trait.php');

        $manager = $this->doctrine->getManager();
        $article = new Article();
        $article->setAuthor('COil');
        self::assertFalse($manager->contains($article));

        $article->setType(ArticleType::BLOG_POST);
        $article->setActive(false);
        $manager->persist($article);
        $manager->flush();
        self::assertTrue($manager->contains($article));

        $manager->remove($article);
        $manager->flush();
        self::assertFalse($manager->contains($article));
    }
}