Inject Symfony EntityManager into Form Type via Services

后端 未结 3 1250
心在旅途
心在旅途 2021-01-13 21:59

I need to modify some fields in my form (label and class), based on whether the entity is the latest published version or not. So I need to be able to inject the entity mana

3条回答
  •  孤城傲影
    2021-01-13 22:18

    declare your form type as service:

    ...
    Gutensite\CmsBundle\Form\Type\ViewVersionType
      
      
    
    ...
    

    in your form type:

    ...
    private $em;
    
    public function __construct(EntityManagerInterface $entityManager) {
        $this->em = $entityManager;
    }
    

    or ou can inject whole container and use it like this:

    $this->container->get('doctrine')->getManager('cms'); // or billing
    

提交回复
热议问题