gpt4 book ai didi

实体中的 Symfony 4 Autowiring 不起作用

转载 作者:行者123 更新时间:2023-12-05 02:15:29 25 4
gpt4 key购买 nike

我正在尝试在我的用户实体 Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface 中 Autowiring 并收到错误函数 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']

最佳答案

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

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

关于实体中的 Symfony 4 Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51798210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com