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

Jér*_*nge 8 php orm symfony deprecation-warning

我正在开发一个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.
Run Code Online (Sandbox Code Playgroud)

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

/**
 * @ORM\Entity
 * @ORM\Table(name="category")
 */
class Category
{
...
Run Code Online (Sandbox Code Playgroud)

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

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

在我service.yaml,我尝试过:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Repository,Tests,Entity/SubDir}'
Run Code Online (Sandbox Code Playgroud)

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

Joe*_*Joe 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}'
Run Code Online (Sandbox Code Playgroud)

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

parameters:
    container.autowiring.strict_mode: true
Run Code Online (Sandbox Code Playgroud)

使用此选项,容器不会尝试使用不作为服务提供的参数创建服务类,您将收到一个决定性错误.这是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 要添加到此答案,此错误是误报:https://github.com/symfony/symfony/issues/25265 (2认同)