How to automatically generate the doctrine repository class of an Entity in Symfony 5

Symfony 5 uses the Doctrine ORM, which makes the things really easy to get started with your new symfony based project. Personally i love to upgrade old projects that uses MySQL databases as it's quite easily to do reverse engineering and build the entities from an already existing database schema.

Doctrine repositories are meant to work as a way to retrieve entities so to speak, you can extend repositories with any method you need to get custom data, for example getUsersWithIntelProcessors or something like that. In this article, we will explain you how to easily generate a custom repository class for an entity in Symfony 5.

Doctrine Repository example in Symfony 5

If you are looking for a way to create quickly a repository for an entity, you can easily copy the following class replacing MyEntity with the name of your entity (even in the filename) and it should work as a custom repository class for your entity:

<?php
// /src/Repository/MyEntityRepository.php
namespace App\Repository;

use App\Entity\MyEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
 * @method MyEntity|null find($id, $lockMode = null, $lockVersion = null)
 * @method MyEntity|null findOneBy(array $criteria, array $orderBy = null)
 * @method MyEntity[]    findAll()
 * @method MyEntity[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MyEntityRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, MyEntity::class);
    }

    // /**
    //  * @return MyEntity[] Returns an array of MyEntity objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?MyEntity
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

Generating Repository class automatically

If you would like to do this automatically with a command, Symfony provides you an easy way to do this as well. As first, be sure to specify on the annotation of your entity, the name of your new repository class, for example:

/**
 * MyEntity
 *
 * @ORM\Table(name="my_entity")
 * @ORM\Entity(repositoryClass="App\Repository\MyEntityRepository")
 */

As you can see, inside the @ORM\Entity() comment, you can specify the repositoryClass property with the namespace of the repository class that will be created. For example with the entire entity class, it should look like this:

<?php

// /src/Entity/MyEntity.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MyEntity
 *
 * @ORM\Table(name="my_entity")
 * @ORM\Entity(repositoryClass="App\Repository\MyEntityRepository")
 */
class MyEntity
{
    // .. 

    // Your entity properties and methods

    // ..
}

Once you verify this, run the following command in your symfony project:

php bin/console make:entity --regenerate App

This should generate an output similar to:

 created: src/Repository/MyEntityRepository.php
 no change: src/Entity/OtherEntitiesYouMayHave.php

  Success! 
           

Done.

It will specify which files were created or modified, in the case of the creation of a custom entity class, you should see a single created line.

Happy coding ❤️!

This could interest you

Become a more social person