Search code examples
symfonydirectorydoctrineentity

Symfony 5 - Make:entity in custom folder?


in my Symfony 5 application I have an architecture like this:

src/
├─ Website/
│  ├─ Entity/
│  ├─ Repository/
│  ├─ Controller/
├─ Application/
│  ├─ Entity/
│  ├─ Repository/
│  ├─ Controller/

When I create an entity with the command php bin/console make:entity, I would like to be able to specify on which folder to create the entity, and that it also creates the repository on the correct folder automatically.

Here is my configuration of the doctrine.yaml file

    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App\Application:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Application/Entity'
                prefix: 'App\Application\Entity'
                alias: Application

            App\Website:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Website/Entity'
                prefix: 'App\Website\Entity'
                alias: Website

For the moment, the only solution I have found is the following:

enter image description here

  1. php bin/console make:entity
  2. I wait for it to ask me for the path, and I enter this if I want to create the entity Totoo in the src/Application/Entity folder : \App\Application\Entity\Totoo
  3. It does create the entity on src/Application/Entity/Totoo.php
  4. But it will create the repository on src/Repository/Application\Entity\TotooRepository.php

How could we optimize this way of creating entities with the command, in order to automatically create the entity and the repository in the right place, and if possible without having to type the full path each time? (Maybe thanks to the aliases in my doctrine setup? But I don't see how)


Solution

  • To perform this do you need to add a maker.yaml file with the following lines (e.g)

    # config/package/dev
    maker:
      root_namespace: 'App\Application\'
    

    When you will type : bin/console make:entity the new entity or modifying an existing Entity will check it out into this folder.

    P:S watch out in which env do you configure it, dev in my case

    That's it ;)