Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Make:entity' failed when don't use the default namespace 'App'. #133

Closed
yuhuaian opened this issue Mar 16, 2018 · 22 comments
Closed

'Make:entity' failed when don't use the default namespace 'App'. #133

yuhuaian opened this issue Mar 16, 2018 · 22 comments
Labels
Bug Bug Fix

Comments

@yuhuaian
Copy link

yuhuaian commented Mar 16, 2018

src/Doctrine/DoctrineEntityHelper.php
line 45
$entityClassDetails = new ClassNameDetails($metadata->name, 'App\\Entity');

src/Maker/MakeCommand.php
line 91
$generator = new Generator($this->fileManager, 'App\\');

@weaverryan
Copy link
Member

Hmm, the command should work if you pass a fully-qualified class name to the first step - e.g. Acme\Foo\Bar, but it looks like we missed at least one thing:

Here's the error I'm currently getting:

./bin/console make:entity

 Class name of the entity to create or update (e.g. GentleGnome):
 > \App\Foo\Bar\Entity\Baz

 created: src/Foo/Bar/Entity/Baz.php
 created: src/Repository/Foo/Bar/Entity/BazRepository.php

 [ERROR] Only annotation mapping is supported by make:entity, but the                            
         <info>App\Foo\Bar\Entity\Baz</info> class uses a different format. If you would like    
         this command to generate the properties & getter/setter methods, add your mapping       
         configuration, and then re-run this command with the <info>--regenerate</info> flag.    

@kilianklein
Copy link

kilianklein commented Mar 31, 2018

Hi guys, first of all thanks for this amazing and promising bundle!

I am testing the bundle with symfony3.4 (as the documentation mentioned it should work with it) and indeed I had to go mess directly with the 2 lignes @yuhuaian mentioned to change "App" to "AppBundle" (unless I am wrong this is the convention for the bundle name for 3.4?). Otherwise, I would not get prompted for the entity's fully qualified name and would get this error instead:

bin/console make:entity Test
Could not determine where to locate the new class "App\Entity\Test".

Hope this helps.

@weaverryan
Copy link
Member

Hmm, this does help! What you’re describing is the expected behavior: we assume App unless you pass a full class name (including the beginning slash iirc). But, I think if we guess App, but we can’t determine where to generate the file (which means that you have no App namespace in your autoloading), we could THEN ask you to clarify by entering the full class name. Then you could easily know how to use the command in these cases :).

@kilianklein
Copy link

Thanks for your answer, the solution sounds good to me! :)

@javiereguiluz javiereguiluz added the Bug Bug Fix label Apr 3, 2018
@helariL
Copy link

helariL commented Apr 30, 2018

Wouldn't it be possible to reference namespace directly from composer.json ? If we anyway have to define it for autoload.

@upyx
Copy link
Contributor

upyx commented May 3, 2018

Hello!

I have the similar issue. We don't use App as root namespace of our project so all make:* commands doesn't work. Few examples:

Command Error
make:auth Could not determine where to locate the new class "App\Security\SuperAuth".
make:command Could not determine where to locate the new class "App\Command\ProjHelloCommand".
make:controller Could not determine where to locate the new class "App\Controller\VictoriousGnomeController".
make:entity Could not determine where to locate the new class "App\Entity\TinyKangaroo".
make:form Could not determine where to locate the new class "App\Form\TinyPizzaType".
make:functional-test Could not determine where to locate the new class "App\Tests\DefaultControllerTest".
make:unit-test Could not determine where to locate the new class "App\Tests\UtilTest".

As we can see all errors are identical. It is because App is hardcoded and there is no way to change it without interfering to code. I'm not sure that simple determining root namespace is possible. But we can do it configurable.

I've found 8 places where App was hardcoded. So refactoring won't be very difficult.

What do you think about?

P.S.
What do you say about changing title to "All 'make:*' failed..."?

@weaverryan
Copy link
Member

@upyx Actually, your description isn't quite right, but clearly the bundle isn't doing a very helpful job :). You can use a non-App namespace on any command, but you must type the full class name. For example, you should be able to

php bin/console make:controller

Then, when it asks you for the controller name, use:

\MyNamespace\Controller\FooController

By starting your namespace with \, we know that you are giving us a full namespace.

We need to:

A) Give a better error message in all commands. Instead of Could not determine where to locate the new class "App\Security\SuperAuth"., we need to suggest that the user type in the full namespace, because obviously, App\ is not used by them.

B) We probably need to make the App\ root prefix configurable.

@LeJeanbono
Copy link
Contributor

Should we do a configuration file ?

@upyx
Copy link
Contributor

upyx commented May 4, 2018

@weaverryan Oh, I didn't know that. Thank you for advice.
However not all commands ask class name. For example, make:command asks command name and doesn't ask class.
I think it would be better if a user does not need to type the full class name.

@LeJeanbono I think so. Would you like to do that?

@helariL
Copy link

helariL commented May 4, 2018

This is not correct @weaverryan

Sure you can use full class name when generating controllers etc. but when generating Entities it doesn't work. You can pass full class name for the Entity but when script reaches the point to start generating the Repository for the Entity it fails and there is no option to pass in the full namespace for that.

Option B sounds reasonable and as i pointed out before, it actually should get the namespace from composer.json and default App namespace should be a fallback when everything else fails.

But otherwise keep up the awesome work!

@eugenekurasov
Copy link

Is any news about repository in Make:entity?

I can do PR if needed.

@upyx
Copy link
Contributor

upyx commented May 4, 2018

@helariL Hello! In my opinion we should use configuration file.
There is part of composer.json from symfony project:

"psr-4": {
    "Symfony\\Bridge\\Doctrine\\": "src/Symfony/Bridge/Doctrine/",
    "Symfony\\Bridge\\Monolog\\": "src/Symfony/Bridge/Monolog/",
    "Symfony\\Bridge\\ProxyManager\\": "src/Symfony/Bridge/ProxyManager/",
    "Symfony\\Bridge\\Twig\\": "src/Symfony/Bridge/Twig/",
    "Symfony\\Bundle\\": "src/Symfony/Bundle/",
    "Symfony\\Component\\": "src/Symfony/Component/"
}

What is it the namespace to use as root? 😄

@helariL
Copy link

helariL commented May 4, 2018

@upyx it was just a suggestion. 😄 Obviously configuration file would be the best option. To be honest I would be happy if we could even pass it in like other options
--namespace=\\MyAwesomeApp\\src ( or something similar )

@eugenekurasov
Copy link

@helariL You can use absolute path (https://github.com/symfony/maker-bundle/blob/master/src/Generator.php#L107)
make:controller \\Controller\\CustomController

@helariL
Copy link

helariL commented May 4, 2018

@eugenekurasov yes that works really well when generating controllers etc. but try the same thing with make:entity 😉

@eugenekurasov
Copy link

eugenekurasov commented May 4, 2018

@helariL make:entity has problem with absolute path.
I created issue for make:entity, but then closed.

weaverryan added a commit that referenced this issue May 6, 2018
This PR was merged into the 1.0-dev branch.

Discussion
----------

Better message with full namespace

#133

Commits
-------

5e9104c Better message with full namespace
weaverryan added a commit that referenced this issue May 17, 2018
…averryan)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Add configuration of root namespace

This PR adds configuration for namespaces used in `make:*` commands that was discussed in #133.
I've used [this doc](https://symfony.com/doc/current/bundles/configuration.html) in work and examples from `FrameworkBundle`.

New parameter:
`maker.root_namespace` - application root namespace, default: `App`

To change it, you should create configuration file in `config/packages/dev` with name `maker.yaml` or `maker.xml` or `maker.php` with root node `maker`. For example YAML file:
```yaml
# config/packages/dev/maker.yaml
maker:
    root_namespace: 'App'
```

I've added some tests. I think we don't need Flex recipe but just document new feature.
I need help for write documentation because my English.

TODO:
- [x] Tests
- [x] Documentations
- [ ] Squash commits

Commits
-------

42b7008 Fixing accidental removal of property
545a422 Merge branch 'master' into configurable_namespace
b92f19c Merge branch 'master' into configurable_namespace
bf1b092 updating for now-missing property
b5658c2 Merge branch 'master' into configurable_namespace
7440ade return type stuff
656116c Changing from an exception
5605d6e adding docs
23a715c Merge remote-tracking branch 'upstream/master' into configurable_namespace
152ffa6 The most important fix
f34fa39 Add exception with hint message when root namespace is not found
758a2c9 Refactor AutoloaderUtil
5423250 Remove maker.root_namespace parameter and improve tests
7157e88 Fix file creation on Windows
657f653 Remove useless code
a425953 Add functional tests for custom root namespace cases
d4139d4 Make "abstract" command and remove entity namespace from config
51d7138 Add configuration of root and entity namespaces
@javiereguiluz
Copy link
Member

Closing as fixed by #173.

@kategray
Copy link

kategray commented Jan 18, 2020

I'm getting the same error, having configured the default namespace. I'm using my own namespace under src/.

ubuntu@aerobill:/var/www/aerobill$ ./bin/console make:entity

 Class name of the entity to create or update (e.g. BraveElephant):
 > Tenant

 created: src/Entity/Tenant.php
 created: src/Repository/TenantRepository.php


 [ERROR] Only annotation mapping is supported by make:entity, but the <info>XXX\Entity\Tenant</info> class uses
         a different format. If you would like this command to generate the properties & getter/setter methods, add your
         mapping configuration, and then re-run this command with the <info>--regenerate</info> flag.
ubuntu@aerobill:/var/www/aerobill$ cat config/packages/dev/maker.yaml
maker:
    root_namespace: 'XXX'

@piotrkardasz
Copy link

For People looking for answer:

It works! To clarify use config similar to this:

config/packages/doctrine.yaml

doctrine:
    orm:
        mappings:
            YourAcme:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'YourAcme\Entity'
                alias: YourAcme

config/packages/dev/maker.yaml

maker:
    root_namespace: YourAcme

@jonmnoj
Copy link

jonmnoj commented Jun 13, 2020

If you are using 3.4 you need to place the maker config in

app/config/config_dev.yml

@weilin-ong
Copy link

For People looking for answer:

It works! To clarify use config similar to this:

config/packages/doctrine.yaml

doctrine:
    orm:
        mappings:
            YourAcme:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'YourAcme\Entity'
                alias: YourAcme

config/packages/dev/maker.yaml

maker:
    root_namespace: YourAcme

Hi, but it only works for 1 specific custom folder, in this case, YourAcme.
What if I want to have multiple custom folders? (e.g YourAcme2, YourAcme3 etc)
I will then have to change the root_namespace manually every time I want to create entity/repository for that specific folder. Is that right?

@upyx
Copy link
Contributor

upyx commented Jan 16, 2023

What if I want to have multiple custom folders? (e.g YourAcme2, YourAcme3 etc) I will then have to change the root_namespace manually every time I want to create entity/repository for that specific folder. Is that right?

No. See #133 (comment)

You can set the full class name like:

bin/console make:entity '\YourAcme2\Entity\SomeEntity'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix
Projects
None yet
Development

No branches or pull requests