Deprecation: Doctrine\ORM\Mapping\UnderscoreNaming

2020-05-21 07:13发布

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

2条回答
Fickle 薄情
2楼-- · 2020-05-21 07:54

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 depreciation 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.

查看更多
Root(大扎)
3楼-- · 2020-05-21 08:04

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])/';
查看更多
登录 后发表回答