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):


<?php declare(strict_types=1);

namespace App\Controller\Snippet;

use App\Entity\Article;
use App\Entity\ArticleType;
use App\Repository\ArticleTypeRepository;
use Doctrine\Bundle\DoctrineBundle\Registry;

/**
 * I am using a PHP trait in order 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.
 */
trait Snippet2Trait
{
    public function snippet2(): void
    {
        // Get doctrine manager (From a Symfony >4.2 controller)
        $doctrine = $this->getDoctrine();
        if (!$doctrine instanceof Registry) {
            throw new \RuntimeException("Houston, We've Got a Problem. 💥");
        }

        $manager = $doctrine->getManager();
        $article = new Article(); // create a fresh Doctrine object
        $isPersisted = $manager->contains($article);
        echo $isPersisted ? 'true' : 'false'; // returns false

        // Set minimum database constraints so the entity can be persisted
        $repo = $manager->getRepository(ArticleType::class);
        if (!$repo instanceof ArticleTypeRepository) {
            throw new \RuntimeException('Doctrine repository not found.');
        }
        $article->setType($repo->getArticleType());
        $article->setIsActive(false);
        // $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!)
        $manager->flush();

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

        // That's it! 😁
    }
}

 Run this snippet  ≪ showUnitTestButtonLabel ≫  More on Stackoverflow  Random snippet

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

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

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

    protected function setUp(): void
    {
        self::bootKernel();
        $this->doctrine = self::$container->get('doctrine');
    }

    /**
     * @covers Snippet2Trait::snippet2()
     *
     * @see https://www.strangebuzz.com/en/snippets/test-if-a-doctrine-entity-is-already-persisted-in-the-database
     */
    public function testSnippet2(): void
    {
        $projectDir = self::$container->getParameter('kernel.project_dir');
        $this->assertFileExists($projectDir.'/src/Controller/Snippet/Snippet2Trait.php');

        $manager = $this->doctrine->getManager();
        $article = new Article();
        $this->assertFalse($manager->contains($article));

        $repo = $manager->getRepository(ArticleType::class);
        if (!$repo instanceof ArticleTypeRepository) {
            throw new \RuntimeException('Doctrine repository not found.');
        }
        $article->setType($repo->getArticleType());
        $article->setIsActive(false);
        $manager->persist($article);
        $manager->flush();
        $this->assertTrue($manager->contains($article));

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