symfony / maker-bundle

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

[ERROR] Only attribute mapping is supported by make:entity, but the <info>App\Entity\User</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. #1152

Closed cmukanisa closed 2 years ago

cmukanisa commented 2 years ago

[ERROR] Only attribute mapping is supported by make:entity, but the App\Entity\User 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 --regenerate flag.

Today, once I updated my project to the latest version of 5.4.* (5.4.10), I can no longer generate the entities nor can I add to those that already exist

image Screenshot 2022-07-15 at 11-15-57 JASSE 🏩🥼 SAINT JOSEPH DE MADAILA PHARMACIE HEALTHCAROLOGY Votre partenaire numérique Remedde ™

cmukanisa commented 2 years ago

`doctrine: dbal: url: '%env(resolve:DATABASE_URL)%'

    # IMPORTANT: You MUST configure your server version,
    # either here or in the DATABASE_URL env var (see .env file)
    #server_version: '13'
orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
        App:
            is_bundle: false
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App

when@test: doctrine: dbal:

"TEST_TOKEN" is typically set by ParaTest

        dbname_suffix: '_test%env(default::TEST_TOKEN)%'

when@prod: doctrine: orm: auto_generate_proxy_classes: false query_cache_driver: type: pool pool: doctrine.system_cache_pool result_cache_driver: type: pool pool: doctrine.result_cache_pool

framework:
    cache:
        pools:
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system

`

MY DOCTRINE.YAML FILE image

NeuralClone commented 2 years ago

I recently ran into this problem as well and was a bit baffled as to what had changed. It looks like annotation support was intentionally removed in v1.44 in favor of PHP 8's native support for metadata using attributes:

https://github.com/symfony/maker-bundle/blob/v1.44.0/CHANGELOG.md (see #1126)

The current Symfony documentation still shows annotations as being supported by MakerBundle, which just adds to the confusion:

https://symfony.com/doc/current/doctrine.html#creating-an-entity-class (for example)

https://github.com/symfony/symfony-docs/issues/16877

I can confirm that this particular feature still works in v1.43. So if you still need annotation support with this bundle, you can limit your dev dependency to <=1.43 and that'll hopefully do the trick. Make sure to run composer update after making that change so it'll downgrade the package. Otherwise, you can read more about attribute mapping in Doctrine here:

https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/attributes-reference.html

cmukanisa commented 2 years ago

Thank you @NeuralClone It's working on downgrading to <=1.43

But I Will need to use attribute in the future, It's will be obligatory to change all annotations in my project to attribute ? It's will be difficult because i have around 300 entities with relationships

NeuralClone commented 2 years ago

Edit: After playing with this some more, it looks like you'll need a custom mapping driver if you want to use both annotations and attributes at the same time:

https://stackoverflow.com/questions/68195822/doctrine-orm-2-9-use-both-annotationdriver-and-attributedriver-to-parse-entity-m

--

You're welcome, @cmukanisa! It seems that everything is headed toward attribute mapping since that's where PHP is at. Symfony is replacing annotations with the PHP approach and Doctrine has been steadily moving that way too.

The syntax for attribute mapping is very similar to annotation mapping. Doctrine's syntax in particular is almost identical to annotations. So it may not be as bad as it seems on the surface.

Rector might be worth a look for converting your existing model over to using attribute mapping.

jrushlow commented 2 years ago

The error is caused by the use of annotations for doctrine entities. Starting w/ v1.44.0 - MakerBundle only supports using attributes with entities. We have a PR up to correct the Symfony docs showing this change.

I highly recommend using rector to upgrade your existing entities from annotations to attributes. I've used this myself in many projects and it works great! Checkout https://getrector.org/blog/how-to-upgrade-annotations-to-attributes for more information. Thanks!

olberger commented 2 years ago

What's expected from users of PHP 7 and Symfony 5.4 ? They'll have to manually downgrade to v1.43 and figure out why the docs mention something that isn't compatible with PHP 7 ?

Symfony 5.4 LTS is supposed to "Requires: PHP 7.2.5 or higher" https://symfony.com/releases/5.4

Heading too fast without a standard dependency on version 1.43 for Symfony 5.4 will render things difficult for existing users on LTS. Yes, they should prepare for upgrade to PHP 8 some day... but upgrades on LTS should be flawless IMHP

wouterj commented 2 years ago

Versions higher than 1.43 require PHP 8.0 in composer.json, so Composer will automatically install 1.43 in a PHP 7.4 environment (unless you run Composer using the --ignore-platform-reqs flag which explicitly disables platform checks from Composer).

olberger commented 2 years ago

Right, maybe users of PHP 7 are fine, then the issue is with Symfony 5.4 and users of PHP 8 then. See numerous reports like https://github.com/symfony/maker-bundle/issues/841 or https://stackoverflow.com/questions/73073080/symfony-5-only-attribute-mapping-is-supported-by-makeentity

wouterj commented 2 years ago

The maker bundle stopped supporting annotations. Supporting multiple formats causes significant complexity and overhead in the maintenance of this bundle. Every application using PHP 8 is recommended to use attributes and PHP 7 has reached end-of-life, so it makes sense to drop support.

If you need annotation support, you can stick to using version 1.43. Maybe the error message can be upgraded to say this, but other than that I think the message is already quite clear.

olberger commented 2 years ago

Well, too bad for LTS... on the other hand, maintaining multiple branches is a pain, I know.

kakhavk commented 2 years ago

I had same problem and solved by adding type field with attribute value like this

      App:
        type: attribute
        ...
marinhekman commented 2 years ago

As @kakhavk mentioned as well.

I still had a "type: annotation" configured in my doctrine configuration. You just have to remove that line or change it to "type: attribute" to set it to use attributes instead

 # config/packages/doctrine.yaml
 doctrine:
     orm:
         mappings:
             App:
                type: annotation # <----------- remove this line
nstCactus commented 1 year ago

I hear your arguments about maintenance of the bundle, PHP 8 support and all. But simply bumping the major version of the bundle to 2.0.0 would have avoided a lot of headaches.

mateusfmello commented 1 year ago

As @kakhavk mentioned as well.

I still had a "type: annotation" configured in my doctrine configuration. You just have to remove that line or change it to "type: attribute" to set it to use attributes instead

 # config/packages/doctrine.yaml
 doctrine:
     orm:
         mappings:
             App:
                type: annotation # <----------- remove this line

Just removing it didn't solve the problem, I had to inform "attribute" in type. As guided by @kakhavk