Symfony 2 EntityManager 注入服务


Symfony 2 EntityManager injection in service

我已经创建了自己的服务,我需要注入原则实体管理器,但我没有看到在我的服务上调用了__construct(),并且注入不起作用。

以下是代码和配置:

<?php
namespace Test'CommonBundle'Services;
use Doctrine'ORM'EntityManager;
class UserService {
    /**
     *
     * @var EntityManager 
     */
    protected $em;
    public function __constructor(EntityManager $entityManager)
    {
        var_dump($entityManager);
        exit(); // I've never saw it happen, looks like constructor never called
        $this->em = $entityManager;
    }
    public function getUser($userId){
       var_dump($this->em ); // outputs null  
    }
}

这是我的捆绑包中的services.yml

services:
  test.common.userservice:
    class:  Test'CommonBundle'Services'UserService
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"

我已经像那样在我的应用程序中导入了config.yml的 .yml

imports:
    # a few lines skipped, not relevant here, i think
    - { resource: "@TestCommonBundle/Resources/config/services.yml" }

当我在控制器中调用服务时

    $userservice = $this->get('test.common.userservice');
    $userservice->getUser(123);

我得到一个对象(不空),但用户服务中的$this->em是空的,正如我已经提到的,用户服务上的构造函数从未被调用过

还有一件事,控制器和UserService在不同的捆绑包中(我真的需要它来保持项目井井有条),但是:其他一切都可以正常工作,我什至可以调用

$this->get('doctrine.orm.entity_manager')

在我用于获取用户服务并获取有效(非空)实体管理器对象的同一控制器中。

看起来我缺少配置或用户服务和原则配置之间的某些链接。

类的构造函数方法应该被称为__construct(),而不是__constructor()

public function __construct(EntityManager $entityManager)
{
    $this->em = $entityManager;
}

对于现代参考,在Symfony 2.4+中,您不能再命名构造函数注入方法的参数。根据您将传入的文档:

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

然后它们将按照通过参数列出的顺序可用(如果超过 1 个)。

public function __construct(EntityManager $entityManager) {
    $this->em = $entityManager;
}
<</div> div class="answers">

注意从Symfony 3.3开始,EntityManager已贬值。请改用实体管理器接口。

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;
        ...
    }
}

自2017年和Symfony 3.3以来,您可以将存储库注册为服务,并具有它的所有优势。

查看我的文章 如何在Symfony中使用带有Doctrine as Service的存储库以获取更一般的描述。


对于您的具体情况,带有调优的原始代码如下所示:

1. 在您的服务或控制器中使用

<?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. 创建新的自定义存储库

<?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. 注册服务

# app/config/services.yml
services:
    _defaults:
        autowire: true
    Test'CommonBundle':
       resource: ../../Test/CommonBundle