Symfony 如何在不破坏CRUD相关操作的情况下将容器正确地注入表单文件?

Symfony 如何在不破坏CRUD相关操作的情况下将容器正确地注入表单文件?,symfony,Symfony,目前,我是这样将容器注入表单的(通过使其成为服务): ApFloorType.php namespace Techforge\ApartmentBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; class ApFloorType e

目前,我是这样将容器注入表单的(通过使其成为服务):

ApFloorType.php
namespace Techforge\ApartmentBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;


class ApFloorType extends AbstractType
{

    //in the controller, they are differently initialised
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    public function buildForm(FormBuilder $builder, array $options)
    {

        $bath_choices = $this->container->getParameter('bath_choices');
        $bed_choices = $this->container->getParameter('bed_choices');

        $builder
            ->add('baths', 'choice', array('choices' => $bath_choices))
            ->add('beds', 'choice', array('choices' => $bed_choices))
            ->add('name')
            ->add('file')
            ->add('plan_description', 'textarea')
            ->add('min_max_feet', 'text')
            ->add('deposit', 'text')
            ->add('application_fee')
            ->add('rental_deposit')
            ->add('lease_terms', 'textarea')
            ->add('threshhold_value')
            ->add('auto_accept')
            ->add('pending')
            ->add('apartment', 'hidden', array('property_path' => false))
            ->add('pre_post_update', 'hidden')
            ->add('photos', 'collection', array('type' => new ApFloorImageType()))

        ;
    }

    public function getName()
    {
        return 'techforge_apartmentbundle_apfloortype';
    }
}
问题是,ApFloor CRUD系统在此之后停止工作,因为在我的控制器中,我不能只写代码

$form   = $this->createForm(new ApFloorType(), $floor);
这将产生以下错误:

可捕获致命错误:参数1传递给 Techforge\ApartmentBundle\Form\ApFloorType::\uu构造()必须 实现接口 Symfony\Component\DependencyInjection\ContainerInterface,未给出任何值, 叫来 /home/storm升降机/webapps/pmr/src/Techforge/ApartmentBundle/Controller/Manager/ApFloorController.php 第38行,并在中定义 /home/storm升降机/webapps/pmr/src/Techforge/ApartmentBundle/Form/ApFloorType.php
namespace Techforge\ApartmentBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;


class ApFloorType extends AbstractType
{

    //in the controller, they are differently initialised
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    public function buildForm(FormBuilder $builder, array $options)
    {

        $bath_choices = $this->container->getParameter('bath_choices');
        $bed_choices = $this->container->getParameter('bed_choices');

        $builder
            ->add('baths', 'choice', array('choices' => $bath_choices))
            ->add('beds', 'choice', array('choices' => $bed_choices))
            ->add('name')
            ->add('file')
            ->add('plan_description', 'textarea')
            ->add('min_max_feet', 'text')
            ->add('deposit', 'text')
            ->add('application_fee')
            ->add('rental_deposit')
            ->add('lease_terms', 'textarea')
            ->add('threshhold_value')
            ->add('auto_accept')
            ->add('pending')
            ->add('apartment', 'hidden', array('property_path' => false))
            ->add('pre_post_update', 'hidden')
            ->add('photos', 'collection', array('type' => new ApFloorImageType()))

        ;
    }

    public function getName()
    {
        return 'techforge_apartmentbundle_apfloortype';
    }
}
第16行

我通过如下调用表单创建方法解决了这个问题

$form   = $this->createForm($this->get('apfloortype'), $floor);

因此,我的问题是,是否有一个解决方法,这样我就不必在CRUD控制器中更改每个createForm()调用,然后将其设置为服务?

这就是我在FormType中所做的:

$data = Class::getYourData();

因为我不知道;I don’我不想(像您一样)修改每个控制器,以便为表单设置额外的参数。

如果您已经将其转换为服务,请进行更改。另一种方法是在创建新表单时始终传递容器:
$this->createForm(newapfloortype($this->container),$floor)另一件事,如果您只需要容器来传递参数,您可以通过服务直接将参数传递给构造函数。