Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relying on service auto-registration error on ORM entity

I am developing a Symfony 3 application. Symfony profiler logs tell me:

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.

Yet, this is a simple ORM bean:

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

How should I get rid of this issue? Do I really need to declare ORM entities as services in services.yaml? If yes, how?

Update In fact, my entity is in a sub directory. I have amended my question.

In my service.yaml, I have tried:

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

...but to no avail.

like image 974
Jérôme Verstrynge Avatar asked Dec 20 '17 14:12

Jérôme Verstrynge


1 Answers

Do you have any Classes under Service-auto registration which use an Entity as constructor argument?

That's where your problem comes from.

You need to ask yourself if the concerning class really is a service or just a plain object of which you always create the instance yourself.

If it is not used as a service through the container you have 2 options:

You can exclude this class also through the glob pattern like for example

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

or you can set the following parameter in your config

parameters:
    container.autowiring.strict_mode: true

with this option the container won't try to create a service class with arguments that are not available as services and you will get a decisive error. This is the default setting for sf4

A good example for a class that triggers exactly this error would be a custom event class that takes an entity as 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 isn't excluded specifically the container will try to auto register it as service. And because the Entity is excluded it can't autowire it. But in 3.4 there's this fallback which triggers this warning. Once the strict_mode is activated the event just won't be available as service and if you tried using it as one an error would rise.

like image 97
Joe Avatar answered Oct 16 '22 23:10

Joe