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

后端 未结 4 839
独厮守ぢ
独厮守ぢ 2020-12-05 15:24

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 ex

相关标签:
4条回答
  • 2020-12-05 15:52

    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

    0 讨论(0)
  • 2020-12-05 15:53

    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
    
    0 讨论(0)
  • 2020-12-05 15:55

    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

    0 讨论(0)
  • 2020-12-05 16:08

    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

    0 讨论(0)
提交回复
热议问题