如何在构造函数中调用实体管理器?

时间:2013-12-14 19:32:52

标签: php symfony constructor entitymanager

我一直试图在构造函数中调用Entity Manager

function __construct()
{
    $this->getDoctrine()->getEntityManager();
    ...

但是,正如我在这个答案中看到的那样:Stackoverflow question,它无法完成。

所以我想知道是否有办法实现它,因为我必须经常调用它,并希望在获取存储库后在构造函数中做一些事情。

修改

我试过@MKhalidJunaid回答:

//src/MSD/HomeBundle/Resources/config/services.yml
services:
  imageTransController.custom.service:
    class:  MSD\HomeBundle\Controller\ImageTransController
    arguments: 
        EntityManager: "@doctrine.orm.entity_manager"

-

//app/config/config.php
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: doctrine_extensions.yml }
- { resource: "@MSDHomeBundle/Resources/config/services.yml" }

-

//src/MSD/HomeBundle/Controller/ImageTransController.php
namespace MSD\HomeBundle\Controller;

use Doctrine\ORM\EntityManager;
use MSD\HomeBundle\Entity\Imagen as Imagen;
use MSD\HomeBundle\Controller\HomeController as HomeController;


class ImageTransController extends HomeController
{
    protected $em ;

    function __construct(EntityManager $entityManager)
    {
    ...

但是我收到了这个错误:

Catchable Fatal Error: Catchable Fatal Error: Argument 1 passed to MSD\HomeBundle\Controller\ImageTransController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /home/manolo/MiServer/itransformer/app/cache/dev/jms_diextra/controller_injectors/MSDHomeBundleControllerImageTransController.php on line 13 and defined in /home/manolo/MiServer/itransformer/src/MSD/HomeBundle/Controller/ImageTransController.php line 38 (500 Internal Server Error)

新尝试:

我也试过@praxmatig回答:

//services.yml
parameters:
 msd.controller.imagetrans.class: MSD\HomeBundle\Controller\ImageTransController

services:
  msd.imagetrans.controller:
    class:  "%msd.controller.imagetrans.class%"
    arguments: [ @doctrine.orm.entity_manager  ]

-

//ImageTransController.php
namespace MSD\HomeBundle\Controller;

 use Doctrine\ORM\EntityManager;

class ImageTransController 
 {
    protected $em ;

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

-

//routing.yml
msd_home_cambiardimensiones:
    pattern: /cambiardimensiones
    defaults: { _controller: MSDHomeBundle:msd.imagetrans.controller:cambiardimensionesAction }

但是我收到了这个错误:

 Unable to find controller "MSDHomeBundle:msd.imagetrans.controller" - class "MSD\HomeBundle\Controller\msd.imagetrans.controllerController" does not exist. (500 Internal Server Error)

6 个答案:

答案 0 :(得分:12)

您需要为您的类提供服务,并将doctrine实体管理器作为参数doctrine.orm.entity_manager传递。就像在services.yml中一样

services:
  test.cutom.service:
    class:  Test\YourBundleName\Yourfoldernameinbundle\Test
    #arguments:
    arguments: [ @doctrine.orm.entity_manager  ] 
        #entityManager: "@doctrine.orm.entity_manager"

您必须在config.yml中导入services.yml

imports:
    - { resource: "@TestYourBundleName/Resources/config/services.yml" }

然后在你的类的构造函数中获取实体管理器作为参数

use Doctrine\ORM\EntityManager;
Class Test {

  protected $em;

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

希望这是有道理的

答案 1 :(得分:5)

将控制器注册为服务时,请勿扩展基本控制器类。有关于它的文档here

class ImageTestController
{
     private $em;

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

     public function someAction()
     {
         // do something with $this->em
     }
}

// services.yml
services:
    acme.controller.image_test:
        class: Acme\SomeBundle\Controller\ImageTestController

// routing.yml
acme:
    path: /
    defaults: { _controller: acme.controller.image_test:someAction }

答案 2 :(得分:1)

为什么要在控制器的构造函数中获取Doctrine 2 EntityManager?

为什么不在您需要的操作中简单地执行$em = $this->getDoctrine()->getManager();(或Symfony 2.0中的$em = $this->getDoctrine()->getEntityManager();)?这样可以避免在不需要时初始化EntityManager的开销。

如果您确实想要这样做,How to define Controllers as Services上有明确的说明。基本上它看起来像这样:

# src/MSD/HomeBundle/Controller/ImageTransController.php

namespace MSD\HomeBundle\Controller;

use Doctrine\ORM\EntityManager;

class ImageTransController
{
    private $em;

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

    public function indexAction()
    {
        // use $this->em
    }
}


# src/MSD/HomeBundle/Resources/config/services.yml

parameters:
    msd.controller.image_trans.class: MSD\HomeBundle\Controller\ImageTransController

services:
    msd.controller.image_trans:
        class:     "%msd.controller.image_trans.class%"
        arguments: ["@doctrine.orm.default_entity_manager"]

# app/config/routing.yml

msd_home_cambiardimensiones:
    path:         /cambiardimensiones
    defaults:     { _controller: msd.controller.image_trans:indexAction }

答案 3 :(得分:0)

你必须添加

use Doctrine\ORM\EntityManager;
控制器中的

答案 4 :(得分:0)

我认为你的方向正确,我会采取第二种选择:

对于第二个选项,我认为routing.yml中的定义是错误的

//routing.yml 
msd_home_cambiardimensiones:
    pattern: /cambiardimensiones
    defaults: { _controller: msd.imagetrans.controller:cambiardimensionesAction }

这里只是从默认值

中的_controller中删除MSDHomeBundle

第一个选项:

HomeController是否有自己的构造函数?

//src/MSD/HomeBundle/Resources/config/services.yml
services:
    imageTransController.custom.service:
        class:  MSD\HomeBundle\Controller\ImageTransController
        arguments: [@doctrine]

它可以帮助然后在构造函数

__construct(Registry $doctrine)
$this->doctrine = $doctrine;

or

$this->em = $doctrine->getManager();

答案 5 :(得分:0)

我看到你试图在控制器的构造函数中获取实体管理器,这不是这样做的方法,除非你打算将控制器定义为服务。 在这种情况下,您需要使用依赖注入来注入服务实体管理器。

但一般来说,在控制器中使用实体管理器的常用方法是使用以下代码获取它:

  $entityManager = $this->container->get('doctrine.orm.entity_manager');