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 :(

leobrl
  • 959
  • 1
  • 6
  • 15
  • 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 '19 at 13:08

2 Answers2

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.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • 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. – Tomek Kobyliński Nov 26 '19 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 '19 at 19:18
  • There is no need for change database schema, jsut need add explicite column name in field declaration. – Tomek Kobyliński Nov 27 '19 at 10:01
  • 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 '19 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: https://github.com/symfony/recipes/blob/master/doctrine/doctrine-bundle/2.0/config/packages/doctrine.yaml – Rvanlaak Dec 04 '19 at 09:07
  • 2
    @Cerad There's something in doctrine's upgrade info: https://github.com/doctrine/orm/blob/2.8.x/UPGRADE.md#deprecated-number-unaware-doctrineormmappingunderscorenamingstrategy I think https://github.com/doctrine/orm/issues/7855 is the relevant issue. – janh Dec 05 '19 at 08:44
  • So how do we get rid of the deprecation log message? I tried to add underscore_number_aware, but returns ServiceNotFoundException – ESP32 Dec 17 '19 at 10:30
  • I assume you mean doctrine.orm.naming_strategy.underscore_number_aware? Make sure your composer update completed cleanly and maybe try the ever popular bin/console cache:clear – Cerad Dec 17 '19 at 14:11
  • Actually, using doctrine.orm.naming_strategy.underscore_number_aware removes the deprecation message as suggested by @Cerad – fgamess Feb 10 '20 at 11:29
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])/';
Niv Penso
  • 333
  • 3
  • 17
  • 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 '20 at 13:20