相关疑难解决方法(0)

Symfony 2 EntityManager注入服务

我已经创建了自己的服务,我需要注入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: …
Run Code Online (Sandbox Code Playgroud)

php dependency-injection symfony

96
推荐指数
4
解决办法
11万
查看次数

标签 统计

dependency-injection ×1

php ×1

symfony ×1