Symfony2 Use Doctrine in Service Container

后端 未结 8 493
猫巷女王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:27

    For Symfony 3.x

    The most easy-peasy solution for me was to just turn on autowiring/autoconfiguring, and then injecting the service I needed via the constructor. Note that I have also allowed any controller to be injected as a service by setting resource: '../../src/AppBundle/*'

     #services.yml or config.yml
     services:
        _defaults:
            autowire: true
            autoconfigure: true
            public: false
        # Allow any controller to be used as a service
        AppBundle\:
            resource: '../../src/AppBundle/*'    
            # you can exclude directories or files
            # but if a service is unused, it's removed anyway
            exclude: '../../src/AppBundle/{Entity,Repository,Tests,DataFixtures,Form}'
    

    Then in any service, you can inject & use the entity manager $em (or any other service/controller) via the constructor like this:

    // class xyz
    private $em;
    // constructor
    public function __construct(\Doctrine\ORM\EntityManager $em)  {
        $this->em = $em;
    }
    public function bar() {
        //Do the Database stuff
        $query = $this->em->createQueryBuilder();
        //Your Query goes here
        $result = $query->getResult();
    }
    
    0 讨论(0)
  • 2020-12-08 13:28

    I am using Symfony 3.4. If you want to create a service in a bundle this works for me:

    services:
     Vendor\YourBundle\Service\YourService:
       arguments:
         $em: '@doctrine.orm.entity_manager'
    

    In your Service.php

    <?php
    
    namespace Hannoma\ElternsprechtagBundle\Service;
    
    use Doctrine\ORM\EntityManager;
    use Hannoma\ElternsprechtagBundle\Entity\Time;
    
    class TimeManager
    {
     protected $em;
    
     public function __construct(EntityManager $em)
     {
        $this->em = $em;
     }
    
    }
    
    0 讨论(0)
  • 2020-12-08 13:31

    for anyone who works with symfony3: u need to do the following inside config/services.yml in order to use doctrine in Service Container:

    servicename_manager:
              class: AppBundle\Services\MyServiceClass
              arguments: [ "@doctrine.orm.entity_manager" ]
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-08 13:43

    For easily accessing the Entitymanager use the following one:

    //services.yml
      your service here:
        class: yourclasshere
        arguments: [@doctrine.orm.entity_manager]
    

    And in the class itself:

    class foo
    {
      protected $em;
    
    
    
      public function __construct(\Doctrine\ORM\EntityManager $em)
      {
        $this->em = $em;
      }
    
      public function bar()
      {
          //Do the Database stuff
          $query = $this->em->createQueryBuilder();
    
          //Your Query goes here
    
          $result = $query->getResult();
      }
    }
    

    This is my first answer so any comments are appreciated :)

    0 讨论(0)
  • 2020-12-08 13:46

    Since 2017 and Symfony 3.3 you can register Repository as service, with all its advantages it has.

    Your code would change like this.

    1. Service configuration

    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        ...\Service\:
           resource: ...\Service
    

    2. Create new class - custom repository:

    use Doctrine\ORM\EntityManagerInterface;
    
    class YourRepository
    { 
        private $repository;
    
        public function __construct(EntityManagerInterface $entityManager)
        {
            $this->repository = $entityManager->getRepository(YourEntity::class);
        }
    
        public function find($id)
        {
            return $this->repository->find($id);
        }
    }
    

    3. Use in any Controller or Service like this

    class dsdsf
    { 
        private $yourRepository;
    
        public function __construct(YourRepository $yourRepository)
        {
            $this->yourRepository = $yourRepository;
        }
    
        public function create()
        {
            $id = 10;
            $this->yourRepository->find($id);
        }
    }
    

    Do you want to see more code and pros/cons lists?

    Check my post How to use Repository with Doctrine as Service in Symfony.

    0 讨论(0)
提交回复
热议问题