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

后端 未结 4 865
独厮守ぢ
独厮守ぢ 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

提交回复
热议问题