symfony / maker-bundle

Symfony Maker Bundle
https://symfony.com/
MIT License
3.35k stars 406 forks source link

make:crud fails to find entity if its name is specified with leading backslash "\" #452

Closed vyshkant closed 2 years ago

vyshkant commented 5 years ago

At this line we don't take into account the leading backslash of an entity, so we first need to remove the backslash from $className and then check if (!\in_array($className, $entities)).

https://github.com/symfony/maker-bundle/blob/4b689109edae80be6598a434e50276f8695dc116/src/Validator.php#L210

LeJeanbono commented 5 years ago

Hi @vyshkant, Can you provide an example to reproduce what you are talking about ? Thank you

vyshkant commented 5 years ago

Sure.

For instance, you moved you Entity directory into Persistence directory, so all your entities are located at App\Persistence\Entity namespace.

To make a CRUD you need to provide full class name (with namespace). In order to avoid preffixing your input with Entity\, you have to specify full namespace starting with a backslash, like this: \App\Persistence\Entity\FooEntity.

Then Validator will check if the class exists (it does): https://github.com/symfony/maker-bundle/blob/4b689109edae80be6598a434e50276f8695dc116/src/Validator.php#L206-L208

In the next step Validator will check whether the $className is in array of $entities https://github.com/symfony/maker-bundle/blob/4b689109edae80be6598a434e50276f8695dc116/src/Validator.php#L210-L212

But the items of an array of $entities are class names without the leading backslash, so the class name \App\Persistence\Entity\FooEntity won't be found, because $entities array will contain App\Persistence\Entity\FooEntity (without leading backslash).

So for the correct check we first need to remove the leading backslash from the $className.

LeJeanbono commented 5 years ago

Did you add in config doctrine.yaml :

doctrine:
    orm:
     mappings:
         #..
         Persistence:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Persistence'
                prefix: 'App\Persistence\Entity'

? Then make:crud with App\Persistence\Entity\FooEntity is ok. You don't need the leading backslash !

jrushlow commented 2 years ago

If this is still an issue, please let us know and we can re-open it. Thanks!