Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

php - How to call Entity Manager in a constructor?

I've been trying to call Entity Manager in a constructor:

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

but, as I've seen in this answer: Stackoverflow question, it can't be done.

So I wonder if there is a way to achieve it, as I have to call it often, and want to do some stuff in the constructor after getting the repository.

Edit:

I've tried with @MKhalidJunaid answer:

//src/MSD/HomeBundle/Resources/config/services.yml
services:
  imageTransController.custom.service:
    class:  MSDHomeBundleControllerImageTransController
    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 MSDHomeBundleController;

use DoctrineORMEntityManager;
use MSDHomeBundleEntityImagen as Imagen;
use MSDHomeBundleControllerHomeController as HomeController;


class ImageTransController extends HomeController
{
    protected $em ;

    function __construct(EntityManager $entityManager)
    {
    ...

but I'm getting this error:

Catchable Fatal Error: Catchable Fatal Error: Argument 1 passed to MSDHomeBundleControllerImageTransController::__construct() must be an instance of DoctrineORMEntityManager, 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)

New attempt:

I've also tried with @praxmatig answer:

//services.yml
parameters:
 msd.controller.imagetrans.class: MSDHomeBundleControllerImageTransController

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

-

//ImageTransController.php
namespace MSDHomeBundleController;

 use DoctrineORMEntityManager;

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 }

but I get this error:

 Unable to find controller "MSDHomeBundle:msd.imagetrans.controller" - class "MSDHomeBundleControllermsd.imagetrans.controllerController" does not exist. (500 Internal Server Error)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make a service for your class and pass the doctrine entity manager as the argument doctrine.orm.entity_manager.Like in services.yml

services:
  test.cutom.service:
    class:  TestYourBundleNameYourfoldernameinbundleTest
    #arguments:
    arguments: [ @doctrine.orm.entity_manager  ] 
        #entityManager: "@doctrine.orm.entity_manager"

You must import your services.yml in config.yml

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

Then in your class's constructor get entity manager as argument

use DoctrineORMEntityManager;
Class Test {

  protected $em;

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

Hope this makes sense


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...