Symfony2 Use Doctrine in Service Container

后端 未结 8 513
猫巷女王i
猫巷女王i 2020-12-08 12:59

How do I use Doctrine in a service container?

The Code just causes an error message \"Fatal error: Call to undefined method ...::get()\".



        
8条回答
  •  一个人的身影
    2020-12-08 13:33

    in the Symfony 3.4. If you want to use Doctrine in a service you can do it: Only this method worked for me

    services.yml:

    YourBundle\PatchService\YourService:
          public: true
          arguments: [ '@doctrine.orm.entity_manager' ]
    

    Service:

    class YourService
    {
        private $em;
        public function __construct($em)  {
            $this->em = $em;
        }
    

    Controller:

    use YourBundle\PatchService\YourService;
    
         /**
             * @Route("/YourController/",name="YourController")
             */
            public function indexAction()
            {
                $em = $this->getDoctrine()->getManager();
                $Notification = new  YourService($em);
    

提交回复
热议问题