Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:  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)
    {
    ...

but I'm getting this error:

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)

New attempt:

I've also tried with @praxmatig answer:

//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 }

but I get this error:

 Unable to find controller "MSDHomeBundle:msd.imagetrans.controller" - class "MSD\HomeBundle\Controller\msd.imagetrans.controllerController" does not exist. (500 Internal Server Error)
like image 694
Manolo Avatar asked Dec 14 '13 19:12

Manolo


People also ask

How do you inject an EntityManager?

You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext without specifying a unitName attribute to use the OC4J default persistence unit, as Example 29-12 shows.

How does EntityManager work in JPA?

In JPA, the EntityManager interface is used to allow applications to manage and search for entities in the relational database. The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit.

Is it possible to inject an entity manager into a constructor?

Currently it's not possible to inject EntityManagers into constructors as @PersistenceContext is defined to not be allowed on parameters. See JPA_SPEC-72 and Allow injecting EntityManagers through constructor injection (and at non-@PersistenceContext injection points in general) [SPR-10443]. Hi Jarek.

How does the EntityManager get a reference to an entity?

The entity manager obtains a reference to an entity either as an argument to a method call or as an entity retrieved from the database. In any case, the EntityManager is the sole arbiter of the entity object.

What is EntityManager in Java Persistence?

The engine supplied by the provider is responsible for the entire Java Persistence API, such as implementing the EntityManager, query classes, and SQL generation. Entity managers are derived from factory interface called EntityManagerFactory. The configuration used by the entity manager is actually created by the entity manager factory.

What is entitymanagerfactory in Salesforce?

Entity managers are derived from factory interface called EntityManagerFactory. The configuration used by the entity manager is actually created by the entity manager factory. This definition is separately called a persistent unit.


1 Answers

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:  Test\YourBundleName\Yourfoldernameinbundle\Test
    #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 Doctrine\ORM\EntityManager;
Class Test {

  protected $em;

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

Hope this makes sense

like image 62
M Khalid Junaid Avatar answered Oct 26 '22 21:10

M Khalid Junaid