我正在开发一个Symfony 3应用程序。Symfony分析器日志告诉我:
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:
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
...我该如何处理这个问题呢?我真的需要在services.yaml中将ORM实体声明为服务吗?如果是,怎么做?
更新实际上,我的实体在一个子目录中。我修正了我的问题。
在我的service.yaml中,我尝试过:
App\:
resource: '../src/*'
exclude: '../src/{Entity,Repository,Tests,Entity/SubDir}'...but无效。
发布于 2017-12-20 14:09:13
您是否有任何在服务自动注册下使用实体作为构造函数参数的类?
这就是你的问题来源。
您需要问自己,所关心的类是否真的是一个服务,还是一个普通的对象,您总是自己创建实例。
如果没有通过容器将其用作服务,则有两个选项:
您也可以通过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被激活,事件将无法作为服务使用,如果您尝试将其作为服务使用,则会出现错误。
https://stackoverflow.com/questions/47907721
复制相似问题