98

我已经创建了自己的服务,我需要注入学说 EntityManager,但我没有看到__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 中为空,正如我已经提到的,从未调用过 UserService 上的构造函数

还有一件事,Controller 和 UserService 在不同的包中(我真的需要它来保持项目的组织性),但仍然:其他一切都很好,我什至可以调用

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

在我用来获取 UserService 并获取有效(非空)EntityManager 对象的同一控制器中。

看起来我缺少配置或 UserService 和 Doctrine 配置之间的某些链接。

4

4 回答 4

112

你的类的构造方法应该被调用__construct(),而不是__constructor()

public function __construct(EntityManager $entityManager)
{
    $this->em = $entityManager;
}
于 2012-05-03T07:58:43.350 回答
65

对于现代参考,在 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;
}
于 2014-07-22T00:09:30.743 回答
18

请注意,从 Symfony 3.3 开始,EntityManager 已折旧。请改用 EntityManagerInterface。

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-08-29T20:22:58.453 回答
7

从 2017 年和 Symfony 3.3开始,您可以将 Repository 注册为服务,并拥有它的所有优势。

查看我的帖子How to use Repository with Doctrine as Service in Symfony以获得更一般的描述。


对于您的具体情况,经过调整的原始代码如下所示:

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
于 2017-10-21T11:03:19.580 回答