依赖ORM实体上的服务自动注册错误

问题描述 投票:8回答:1

我正在开发一个Symfony 3应用程序。 Symfony profiler日志告诉我:

Relying on service auto-registration for type "App\Entity\SubDir\Category"
is deprecated since version 3.4 and won't be supported in 4.0.
Create a service named "App\Entity\SubDir\Category" instead.

然而,这是一个简单的ORM bean:

/**
 * @ORM\Entity
 * @ORM\Table(name="category")
 */
class Category
{
...

我应该如何摆脱这个问题?我是否真的需要在services.yaml中将ORM实体声明为服务?如果有,怎么样?

更新实际上,我的实体位于子目录中。我修改了我的问题。

在我的service.yaml中,我尝试过:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Repository,Tests,Entity/SubDir}'

......但无济于事。

php symfony orm deprecation-warning
1个回答
8
投票

在Service-auto注册下是否有使用Entity作为构造函数参数的任何类?

这就是你的问题所在。

你需要问问自己,相关的类是真的是一个服务,还是只是一个你总是自己创建实例的普通对象。

如果它没有通过容器用作服务,您有2个选项:

您也可以通过glob模式排除此类,例如

AppBundle\:
    resource: '...'
    # you can exclude directories or files
    # but if a service is unused, it's removed anyway
    exclude: '../../{Entity,PathToYourNotService}'

或者您可以在配置中设置以下参数

parameters:
    container.autowiring.strict_mode: true

使用此选项,容器不会尝试使用不作为服务提供的参数创建服务类,您将收到一个决定性错误。这是sf4的默认设置

对于完全触发此错误的类,一个很好的示例是一个自定义事件类,它将实体作为构造函数中的有效负载:

namespace AppBundle\Event;

use AppBundle\Entity\Item;
use Symfony\Component\EventDispatcher\Event;

class ItemUpdateEvent extends Event
{
    const NAME = 'item.update';

    protected $item;

    public function __construct(Item $item)
    {
        $this->item = $item;
    }

    public function getItem()
    {
        return $this->item;
    }
}

现在,如果没有特别排除此文件,容器将尝试将其自动注册为服务。并且因为实体被排除在外,所以它不能自动装配它。但是在3.4中有这个回退触发了这个警告。一旦激活了strict_mode,该事件将无法作为服务使用,如果您尝试将其用作一个,则会出现错误。

© www.soinside.com 2019 - 2024. All rights reserved.