Symfony 4 and Doctrine, how to generate repository

2020-02-09 03:04发布

问题:

All the tutorials I am finding have the repository created automatically using make:entity when creating new tables

but I have been importing from an existing database following the official documentation with the following command: php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity

This command does not seem to create any repository... and the documentation does not talk about generating a repository

I know I can create one manually but is there a command to generate them automatically ? I have 25 tables.... Would be very useful

lazy me oO

edit: I also tried php bin\console make:entity --regenerate but I get no change on all tables and no repository created

回答1:

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.

---> 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
{

}

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



回答2:

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



回答3:

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


回答4:

Do not copy the annotation too fast, I copied the annotation, but in my case the entities were generated automatically, so there was already a line ORM\Repository, which appeared after, delete it or replace it with the solution