Symfony 2 EntityManager注入服务

And*_*rin 96 php dependency-injection symfony

我已经创建了自己的服务,我需要注入doctrine 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  
    }

}
Run Code Online (Sandbox Code Playgroud)

这是services.yml我的捆绑

services:
  test.common.userservice:
    class:  Test\CommonBundle\Services\UserService
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"
Run Code Online (Sandbox Code Playgroud)

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

imports:
    # a few lines skipped, not relevant here, i think
    - { resource: "@TestCommonBundle/Resources/config/services.yml" }
Run Code Online (Sandbox Code Playgroud)

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

    $userservice = $this->get('test.common.userservice');
    $userservice->getUser(123);
Run Code Online (Sandbox Code Playgroud)

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

还有一件事,Controller和UserService在不同的包中(我真的需要它来保持项目的组织),但仍然:其他工作正常,我甚至可以调用

$this->get('doctrine.orm.entity_manager')
Run Code Online (Sandbox Code Playgroud)

在我用来获取UserService并获得有效(非null)EntityManager对象的同一个控制器中.

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

ric*_*age 112

应调用类的构造函数方法__construct(),而不是__constructor():

public function __construct(EntityManager $entityManager)
{
    $this->em = $entityManager;
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨,在此示例中,如何将连接从默认更改为其他任何连接? (2认同)

Cha*_*yer 65

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

services:
    test.common.userservice:
        class:  Test\CommonBundle\Services\UserService
        arguments: [ "@doctrine.orm.entity_manager" ]
Run Code Online (Sandbox Code Playgroud)

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

public function __construct(EntityManager $entityManager) {
    $this->em = $entityManager;
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以执行:app/console container:debug并找出您正在运行的服务. (8认同)

Rob*_*lor 17

注意,从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;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*uba 7

自2017年和Symfony 3.3以来,您可以将Repository注册为服务,具有其所具有的所有优势.

查看我的文章如何在Symfony中将Repository与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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

3.注册服务

# app/config/services.yml
services:
    _defaults:
        autowire: true

    Test\CommonBundle\:
       resource: ../../Test/CommonBundle
Run Code Online (Sandbox Code Playgroud)