Symfony2: How call the repository of an entity in the FormType

后端 未结 2 653
走了就别回头了
走了就别回头了 2021-01-26 06:52

I tried to call the repository of my entity Category in the class form of my entity BonCommande, but this error ocuured:

Notice:

相关标签:
2条回答
  • 2021-01-26 07:25

    FormComponent is an independent component and it doesn't provide any entityManager to use. You have to inject it or pass it by $options if you want to use it..

    In your case it would be correct if you directly pass it to the type's __construct or pass by $options array or declare your type as a service and inject entity manager to it:

    class BonCommandeType extends AbstractType
    {
        private $em;
    
        public function __construct(EntityManagerInterface $em)
        {
            $this->em = $em;
        }
    
        ...
    }
    

    or

    $this->createForm(TYPE, DATA, ['em' => $em]);
    
    0 讨论(0)
  • 2021-01-26 07:29

    From your code I assume you are missing this:

    //Somewhere at the begging of your BonCommandeType
    protected $em;
    ...
    public function __construct(EntityManager $em) 
    {
        $this->em = $em;
    }
    

    Keep in mind that when you create a new form object you should use smth like :

    BonCommandeType($this->getDoctrine()->getManager()) // if inside a controller
    
    0 讨论(0)
提交回复
热议问题