实体中的Symfony 4自动装配不起作用

时间:2018-08-11 09:12:49

标签: symfony entity autowired symfony4

我正在尝试在我的用户实体Symfony \ Component \ Security \ Core \ Encoder \ UserPasswordEncoderInterface中自动布线 并收到错误,函数App \ Entity \ User :: __ construct()的参数太少。

public function __construct(UserPasswordEncoderInterface $encoder)
{
    $this->packages = new ArrayCollection();
    $this->passwordEncoder = $encoder;
}

在我的services.yaml

services:
# default configuration for services in *this* file
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    public: false       # Allows optimizing the container by removing unused services; this also means
                        # fetching services directly from the container via $container->get() won't work.
                        # The best practice is to be explicit about your dependencies anyway.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

1 个答案:

答案 0 :(得分:4)

如评论中所述,实体不被视为服务,因此不能使用自动装配。

应该将实体用于简单的数据操作。如果您需要访问其他Symfony组件,则可以选择的一个方法是创建一个管理器类来处理其他工作或依赖事件侦听器。