Symfony 2 EntityManager injection in service

后端 未结 4 999
栀梦
栀梦 2020-11-28 23:21

I\'ve created my own service and I need to inject doctrine EntityManager, but I don\'t see that __construct() is called on my service, and injection doesn\'t wo

相关标签:
4条回答
  • 2020-11-28 23:59

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

    Check my post How to use Repository with Doctrine as Service in Symfony for more general description.


    To your specific case, original code with tuning would look like this:

    1. Use in your services or Controller

    <?php
    
    namespace Test\CommonBundle\Services;
    
    use Doctrine\ORM\EntityManagerInterface;
    
    class UserService
    {
        private $userRepository;
    
        // use custom repository over direct use of EntityManager
        // see step 2
        public function __constructor(UserRepository $userRepository)
        {
            $this->userRepository = $userRepository;
        }
    
        public function getUser($userId)
        {
            return $this->userRepository->find($userId);
        }
    }
    

    2. Create new custom repository

    <?php
    
    namespace Test\CommonBundle\Repository;
    
    use Doctrine\ORM\EntityManagerInterface;
    
    class UserRepository
    {
        private $repository;
    
        public function __construct(EntityManagerInterface $entityManager)
        {
            $this->repository = $entityManager->getRepository(UserEntity::class);
        }
    
        public function find($userId)
        {
            return  $this->repository->find($userId);
        }
    }
    

    3. Register services

    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        Test\CommonBundle\:
           resource: ../../Test/CommonBundle
    
    0 讨论(0)
  • 2020-11-29 00:02

    Note as of Symfony 3.3 EntityManager is depreciated. Use EntityManagerInterface instead.

    namespace AppBundle\Service;
    
    use Doctrine\ORM\EntityManagerInterface;
    
    class Someclass {
        protected $em;
    
        public function __construct(EntityManagerInterface $entityManager)
        {
            $this->em = $entityManager;
        }
    
        public function somefunction() {
            $em = $this->em;
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-29 00:12

    Your class's constructor method should be called __construct(), not __constructor():

    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
    }
    
    0 讨论(0)
  • 2020-11-29 00:12

    For modern reference, in Symfony 2.4+, you cannot name the arguments for the Constructor Injection method anymore. According to the documentation You would pass in:

    services:
        test.common.userservice:
            class:  Test\CommonBundle\Services\UserService
            arguments: [ "@doctrine.orm.entity_manager" ]
    

    And then they would be available in the order they were listed via the arguments (if there are more than 1).

    public function __construct(EntityManager $entityManager) {
        $this->em = $entityManager;
    }
    
    0 讨论(0)
提交回复
热议问题