【问题标题】:Deprecation: Doctrine\ORM\Mapping\UnderscoreNamingStrategy without making it number aware is deprecated弃用:不建议使用 Doctrine\ORM\Mapping\UnderscoreNamingStrategy 而不使其知道数字
【发布时间】:2019-11-21 12:33:35
【问题描述】:

我正在使用 Symfony 4.3.8,但找不到任何关于这些弃用的信息:

不推荐使用用户:不推荐使用创建 Doctrine\ORM\Mapping\UnderscoreNamingStrategy 而不使其知道数字的做法,并将在 Doctrine ORM 3.0 中删除。

不建议使用创建 Doctrine\ORM\Mapping\UnderscoreNamingStrategy 而不使其知道数字,并将在 Doctrine ORM 3.0 中删除。

我在stacktrace中搜索并找到了这个:

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;
}

在这个类中,构造函数总是在没有参数的情况下被调用,所以 $numberAware 总是 false。

这个类在 Symfony 依赖注入自动生成的文件中被调用,所以我不能“编辑”它...

我想可能是在教义.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

但我还没有找到任何允许数字感知的选项:(

【问题讨论】:

  • 只需创建一个新的 4.4.0(刚刚发布,是的)项目,并且教义.yaml 中包含“naming_strategy:教义.orm.naming_strategy.underscore_number_aware”。尝试调整你的。

标签: symfony doctrine-orm doctrine


【解决方案1】:

在大多数情况下,我只会用评论来回答这类问题,但我怀疑其他开发人员可能会遇到这个问题。我查了一下,找不到任何关于这个问题的明确文档。也许是因为 DoctrineBundle 是在 Doctrine 人员而不是 Symfony 开发人员的控制之下。或者我只是一个糟糕的搜索者。

无论如何,在 4.3 和 4.4 之间,下划线命名策略的服务名称发生了变化。

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

并添加了一条已弃用的消息,以警告开发人员更改名称。如果消息更明确一点,那就太好了,但哦,好吧。 因此,如果您要将现有应用升级到 4.4 及更高版本,则您可能需要手动编辑您的学说.yaml 文件以消除折旧消息。

更多信息(感谢@janh)为什么做出改变: https://github.com/doctrine/orm/blob/2.8.x/UPGRADE.md#deprecated-number-unaware-doctrineormmappingunderscorenamingstrategy https://github.com/doctrine/orm/issues/7855

仍然不清楚为什么“他们”选择以这种方式做事,但哦,好吧。 您可能想运行“bin/console dictionary:schema:update --dump-sql”来查看这是否会影响您的数据库列名并进行相应调整。更改已经发布了几个星期,似乎没有多少人对更改感到愤怒,所以我猜大多数列名都没有嵌入数字。至少到目前为止。

【讨论】:

  • 旧策略更改(错误)例如 $singleMd5Key 到 single_payu_md5key 和新策略(正确) single_payu_md5_key。但因为那是公元前的变化,我们有那么多的混乱。
  • @TomekKobyliński 除了代码本身之外,您还能找到任何关于此的文档吗?当 Doctrine 3 到来时,仍然难以理解为什么命名约定会改变(并因此可能会强制改变数据库模式)。似乎两种方法都会受到支持。
  • 因此,您必须手动更新实体映射,而不是强制更改数据库架构?不确定哪个更糟,它并没有真正解决为什么要改变的问题。提供更“正确”的策略没有问题,但我仍然不明白为什么原始策略在任何相关意义上都是“错误”的。
  • 在深入研究此弃用(通过运行 phpunit 找到)后来到这里。最好在答案中链接到配方 yaml,因为这证实了建议的修复:github.com/symfony/recipes/blob/master/doctrine/doctrine-bundle/…
  • @Cerad 在教义的升级信息中有些东西:github.com/doctrine/orm/blob/2.8.x/… 我认为github.com/doctrine/orm/issues/7855 是相关问题。
【解决方案2】:

对于那些使用 symfony4.3 并且仍然希望此警告消失的用户,您可以在 service.yaml

中添加新的服务定义
    custom_doctrine_orm_naming_strategy_underscore:
    class: Doctrine\ORM\Mapping\UnderscoreNamingStrategy
    arguments:
        - 0
        - true

并像这样更改 doctrine.yaml 的配置:

orm:
    naming_strategy: custom_doctrine_orm_naming_strategy_underscore

在直接提交此更改之前,我建议您验证将 true 传递给 Doctrine\ORM\Mapping\UnderscoreNamingStrategy 不会影响代码的预期行为。

// 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;
}

快速提示:

true 传递给c'tor 将使类使用NUMBER_AWARE_PATTERN 而不是DEFAULT_PATTERN

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

【讨论】:

  • 如果你还是要切换到新的数字感知策略,那么为什么不直接在 Doctrine 配置中更新策略而不是使用这个 hack?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2022-10-26
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多