Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 985
Sam Avatar asked Aug 09 '18 18:08

Sam


3 Answers

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

like image 63
Elbarto Avatar answered Nov 16 '22 13:11

Elbarto


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
like image 38
faye.babacar78 Avatar answered Nov 16 '22 13:11

faye.babacar78


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

like image 5
rajkumarsakthi Avatar answered Nov 16 '22 15:11

rajkumarsakthi