Symfony 4 and Doctrine, how to generate repository automatically after mapping?

How to Generate Entities from an Existing Database

Table name: CamelCase (eg: table_name will be TableName)

php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity --filter="TableName"

How to Generate Entities

Run below command, it will create entity file.

php bin/console make:entity --regenerate

Next, go to your entity file and add @ORM\Entity repositoryClass

Example Entity file

/**
 * XXXXXX
 *
 * @ORM\Table(name="XXXX")
 * @ORM\Entity(repositoryClass="App\Repository\XXXXRepository")
 */

class XXXXX {

Run again this command again, and it will create repository for you.

php bin/console make:entity --regenerate


SOLUTION 1

You can simply run

php bin\console make:entity --regenerate

This will prompt and ask for:

Enter a class or namespace to regenerate [App\Entity]:

Just press Enter or specify the location of your entity folder, and it will create missing getters/setters & Repositories.

test

---> WARNING:
If it does not create the repositories make sure you have the following annotation in your entities :

/**
 * @ORM\Entity(repositoryClass="App\Repository\MyClassRepository")
 */
class MyClass
{

}

You also might want to clear your cache if it's not working (as noted by @Pavel Petrov in the comments)

SOLUTION 2

The SymfonyMakerBundle allows you to create your own makers. So you could make a new one called make:repositories that will generate a repository for each entity found in the /Entity folder.

To do that, create a class (MakeRepositories) that extends AbstractMaker in your src/Maker/ directory. (documentation: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#creating-your-own-makers)

Use the core maker make:entity to help you create your new command (since it contains the code to generate a repository) : https://github.com/symfony/maker-bundle/blob/master/src/Maker/MakeEntity.php


After generating your entity classes from database, add the following annotation to each of your entities:

@ORM\Entity(repositoryClass="App\Repository\ClassNameRepository")

To genenerate the repository classes, run the following command:

php bin/console make:entity --regenerate App