87

I'm using Symfony 4.3.8 and I can't find any information about thoses deprecations :

User Deprecated: Creating Doctrine\ORM\Mapping\UnderscoreNamingStrategy without making it number aware is deprecated and will be removed in Doctrine ORM 3.0.

Creating Doctrine\ORM\Mapping\UnderscoreNamingStrategy without making it number aware is deprecated and will be removed in Doctrine ORM 3.0.

I searched in stacktrace and found this :

class UnderscoreNamingStrategy implements NamingStrategy
{
private const DEFAULT_PATTERN      = '/(?<=[a-z])([A-Z])/';
private const NUMBER_AWARE_PATTERN = '/(?<=[a-z0-9])([A-Z])/';

/**
 * Underscore naming strategy construct.
 *
 * @param int $case CASE_LOWER | CASE_UPPER
 */
public function __construct($case = CASE_LOWER, bool $numberAware = false)
{
    if (! $numberAware) {
        @trigger_error(
            'Creating ' . self::class . ' without making it number aware is deprecated and will be removed in Doctrine ORM 3.0.',
            E_USER_DEPRECATED
        );
    }

    $this->case    = $case;
    $this->pattern = $numberAware ? self::NUMBER_AWARE_PATTERN : self::DEFAULT_PATTERN;
}

In this class, the constructor is always called without params, so $numberAware is always false.

This class is called in file which has been auto generated by the Symfony Dependency Injection, so I can't "edit" it ...

I thought maybe it was in doctrine.yaml :

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

But I have not found any option to allow the number aware :(

1
  • 5
    Just make a fresh 4.4.0 (just released, yea) project and doctrine.yaml has "naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware" in it. Try tweaking yours.
    – Cerad
    Nov 21, 2019 at 13:08

2 Answers 2

193

In most cases I would just answer this sort of question with a comment but I suspect other developers might run into this issue. I poked around a bit and could not find any explicit documentation on this issue. Perhaps because the DoctrineBundle is under the control of the Doctrine folks and not the Symfony developers. Or maybe I am just a bad searcher.

In any event, between 4.3 and 4.4 the service name for the underscore naming strategy was changed.

# doctrine.yaml
orm:
  # 4.3
  naming_strategy: doctrine.orm.naming_strategy.underscore
  # 4.4
  naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware

And a deprecated message was added to warn developers to change the name. Would have been nice if the message was just a tiny bit more explicit but oh well. So if you are upgrading an existing app to 4.4 and beyond then you will probably need to manually edit your doctrine.yaml file to make the depreciation message go away.

Some more info (thanks @janh) on why the change was made: https://github.com/doctrine/orm/blob/2.8.x/UPGRADE.md#deprecated-number-unaware-doctrineormmappingunderscorenamingstrategy https://github.com/doctrine/orm/issues/7855

Still not really clear on why "they" chose to do things this way but oh well. You probably want to run "bin/console doctrine:schema:update --dump-sql" just to see if this impacts your database column names and adjust accordingly. The changes has been out for several weeks now and there does not seem to be many howls of outrage over the change so I guess most column names don't have embedded numbers. So far at least.

10
  • old strategy change (wrongly) for example $singleMd5Key to single_payu_md5key and new one (correctly) single_payu_md5_key. but coz that is BC change we have all that mess. Nov 26, 2019 at 16:39
  • @TomekKobyliński Have you been able to find any documentation on this besides the code itself? Still struggling to understand why the naming convention would change (and thus possibly force a database schema change) when Doctrine 3 arrives. Just seems like both approaches would be supported.
    – Cerad
    Nov 26, 2019 at 19:18
  • 1
    So instead of forcing a database schema change you have to manually update your entity mappings? Not sure which is worse and it doesn't really address the question of why change at all. No problem with providing a more "correct" strategy but I still don't understand why the original strategy is "wrong" in any relevant sense.
    – Cerad
    Nov 27, 2019 at 12:56
  • 1
    Came here after diving into this deprecation (found by running phpunit) as well. Would be good to link to the recipe yaml in the answer, as that confirms the proposed fix: github.com/symfony/recipes/blob/master/doctrine/doctrine-bundle/…
    – Rvanlaak
    Dec 4, 2019 at 9:07
  • 2
    @Cerad There's something in doctrine's upgrade info: github.com/doctrine/orm/blob/2.8.x/… I think github.com/doctrine/orm/issues/7855 is the relevant issue.
    – janh
    Dec 5, 2019 at 8:44
1

For those who works with symfony4.3 and still want this warning disapear you can add add new new service defintion in service.yaml

    custom_doctrine_orm_naming_strategy_underscore:
    class: Doctrine\ORM\Mapping\UnderscoreNamingStrategy
    arguments:
        - 0
        - true

and change the configuration of doctrine.yaml like this:

orm:
    naming_strategy: custom_doctrine_orm_naming_strategy_underscore

before going straight forward committing this change I would suggest you to verify that passing true to the Doctrine\ORM\Mapping\UnderscoreNamingStrategy doesn't affect the expected behavior of your code.

// class UnderscoreNamingStrategy
/**
 * Underscore naming strategy construct.
 *
 * @param int $case CASE_LOWER | CASE_UPPER
 */
public function __construct($case = CASE_LOWER, bool $numberAware = false)
{
    if (! $numberAware) {
        @trigger_error(
            'Creating ' . self::class . ' without making it number aware is deprecated and will be removed in Doctrine ORM 3.0.',
            E_USER_DEPRECATED
        );
    }

    $this->case    = $case;
    $this->pattern = $numberAware ? self::NUMBER_AWARE_PATTERN : self::DEFAULT_PATTERN;
}

Quick hint:

passing true to the c'tor will make the class use the NUMBER_AWARE_PATTERN instead of the DEFAULT_PATTERN

private const DEFAULT_PATTERN      = '/(?<=[a-z])([A-Z])/';
private const NUMBER_AWARE_PATTERN = '/(?<=[a-z0-9])([A-Z])/';
1
  • 1
    If you're switching to the new number-aware strategy anyway, then why not just update the strategy in Doctrine config instead of using this hack?
    – gronostaj
    Jul 22, 2020 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.