I'm developing a Symfony 3 application. Symfony profiler log tells me:
依赖于服务自动注册类型"AppEntitySubDirCategory"已自3.4版本起被弃用,并且在4.0版本中将不再支持。 请创建一个名为"AppEntitySubDirCategory"的服务。
However, this is just a simple ORM bean:
/** * @ORMEntity * @ORMTable(name="category") */ class Category { ...
How should I solve this problem? Do I really need to declare the ORM entity as a service in services.yaml
? If yes, what should be done?
UPDATE In fact, my entities are in a subdirectory. I've modified my question.
In my service.yaml
I tried:
App: resource: '../src/*' exclude: '../src/{Entity,Repository,Tests,Entity/SubDir}'
...But it has no effect.
P粉2810894852023-11-06 19:06:16
Do you have a class under Service-auto registration that uses entities as constructor parameters?
This is where your problem lies.
You need to ask yourself whether this related class is really a service, or just an ordinary object that you always create instances yourself.
If it is not used as a service through a container, you have two options:
You can exclude this class through a global pattern similar to the following:
AppBundle\: resource: '...' # 您可以排除目录或文件 # 但如果一个服务未使用,它会被移除 exclude: '../../{Entity,PathToYourNotService}'
Or you can set the following parameters in your configuration:
parameters: container.autowiring.strict_mode: true
With this option, the container will not try to create a service class with parameters that are not available as a service, and you will get a decisive error. This is the default setting for sf4.
A good example that triggers this error is a custom event class that takes an entity as a payload in the constructor:
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; } }
Now, if this file is not specifically excluded, the container will attempt to automatically register it as a service. Since the solid body is excluded, it cannot be autowired. But in 3.4, there is this fallback mechanism that triggers this warning. Once strict_mode is activated, the event is not available as a service and will throw an error if you try to use it as a service.