-1

我正在将应用程序从 Symfony 4.4 迁移到 Symfony 5.4。在表单的情况下,我有一个表单,我在构造函数中传递了一个变量ContainerInterface $container,以在小部件的配置中使用参数。在 5.4 版中,这给了我一个弃用警告:

从 symfony/dependency-injection 5.1 开始:“Symfony\Component\DependencyInjection\ContainerInterface”自动装配别名已被弃用。如果您想继续使用它,请在您的应用程序中明确定义它。它被“App\Form\BuscarAvanzadaNinhoType”服务引用。

我该如何解决?

class BuscarAvanzadaNinhoType extends AbstractType {

    private $em;
    private $security;
    private $uuidEncoder;
    private $container;

    public function __construct(ManagerRegistry $em, Security $security, UuidEncoder $uuidEncoder, ContainerInterface $container) {
        $this->em = $em;
        $this->security = $security;
        $this->uuidEncoder = $uuidEncoder;
        $this->container = $container;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
               
                ->add('otrosAspectos', Select2EntityType::class, array(
                    'class' => EtiquetaAspectoNinho::class,
                    'remote_route' => 'ninhos_encontrarEtiquetaAspectos',
                    'primary_key' => 'id',
                    'text_property' => 'text',
                    'multiple' => true,
                    'allow_clear' => false,
                    'delay' => 250,
                    'cache' => false,
                    'minimum_input_length' => 3,
                    'scroll' => true,
                    'page_limit' => $this->container->getParameter('limite_resultados_etiquetas'),
                    'language' => 'es',
                    'width' => '100%',
                    'placeholder' => '',
                        )
        );

        
    }

    

}
4

2 回答 2

1

您可以不使用容器直接传递参数。

public function __construct(ManagerRegistry $em, Security $security, UuidEncoder $uuidEncoder, array $formParams) {
    $this->em = $em;
    $this->security = $security;
    $this->uuidEncoder = $uuidEncoder;
    $this->formParams = $formParams;
}

服务.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            $formParams:
                limite_resultados_etiquetas: '%limite_resultados_etiquetas%'

在表格中:

'page_limit' => $this->formParams['limite_resultados_etiquetas'],
于 2022-02-01T08:05:12.163 回答
-1

不要使用 ContainerInterface,而是使用 ContainerBagInterface

于 2022-02-01T08:32:04.970 回答