Symfony 2 Entitymanager Injection in Service

Symfony 2 EntityManager injection in service

Your class's constructor method should be called __construct(), not __constructor():

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

Is there a way to inject EntityManager into a service

Traditionally, you would have created a new service definition in your services.yml file set the entity manager as argument to your constructor

app.the_service:
class: AppBundle\Services\TheService
arguments: ['@doctrine.orm.entity_manager']

More recently, with the release of Symfony 3.3, the default symfony-standard-edition changed their default services.yml file to default to using autowire and add all classes in the AppBundle to be services. This removes the need for adding the custom service and using a type hint in your constructor will automatically inject the right service.

Your service class would then look like the following:

use Doctrine\ORM\EntityManagerInterface;

class TheService
{
private $em;

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

// ...
}

For more information about automatically defining service dependencies, see https://symfony.com/doc/current/service_container/autowiring.html

The new default services.yml configuration file is available here: https://github.com/symfony/symfony-standard/blob/3.3/app/config/services.yml

How to use mulitple entity manager in services symfony 4

You can try to inject Doctrine\Common\Persistence\ManagerRegistry in your construct. And then use $managerRegistry->getManager('your_connection_name');

For example:

//use Doctrine\Common\Persistence\ManagerRegistry;
private $connection;
function __construct(ManagerRegistry $em)
{
$this->connection = $em->getManager('your_connection_name');
}

symfony2 SoapServer EntityManager injection in service

Instead of using SoapServer::setClass() you could use SoapServer::setObject() and pass your service in:

$oSOAPServer = new \SoapServer('/path/to/wsdl');
$oSOAPServer->setObject($container->get('my_service'));
$oSOAPServer->handle();

Implementing a soap server is documented in the Symfony documentation: How to Create a SOAP Web Service in a Symfony2 Controller

Also, if you only need a repository, don't inject the whole entity manager. Register your repository as a service and inject it to your service.

How to inject specific Doctrine Entity Manager in Symfony2?

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager
@doctrine.orm.sub_entity_manager

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager:
arguments: [ @doctrine.orm.sub_entity_manager ]

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services:
acme.foo_repository:
class: Doctrine\Common\Persistence\ObjectRepository
factory_service: doctrine.orm.main_entity_manager
factory_method: getRepository
arguments:
- "AcmeMainBundle:Foo"

We let doctrine generate the given repository.

Then we can inject it in another service

services:
mainmanager:
class: Acme\MainBundle\MainManager
arguments:
- @acme.foo_repository

Dependency Inject non-default Entity Manager into Service in Symfony

If your service class just needs the connection then it easiest way it make your own connection class and inject it.

namepace AppBundle\Connection;

class PsConnection extends Doctrine\DBAL\Connection
{
}

# doctrine.yaml
doctrine:
dbal:
connections:
ps:
wrapper_class: AppBundle\Connection\PsConnection

# services.yaml
App\:
resource: '../src/'
exclude:
- '../src/AppBundle/Connection/PsConnection.php'

class HilaService
{
public function __construct(AppBundle\Connection\PsConnection $conn)

Everything will work as before but you can get the connection directly.

if you really do need the entity manager then you can make a service definition:

# services.yaml
AppBundle\Service\HilaService:
$entityManager: '@doctrine.orm.ps_entity_manager'

Finally, if you don't want to fool around with any of this stuff you can inject the ManagerRegistry and pull what you need from it.

class HilaService
{
public function __construct(Doctrine\Common\Persistence\ManagerRegistry $managerRegistry)
{
$em = $managerRegistry->getManager('ps'); // or getConnection()

Inject EntityManagerInterface to my service with `must implement interface Doctrine\ORM\EntityManagerInterface error`

You do not need binding $em to the Doctrine entity manager.

If you remove that line and leave the type hint only (__construct(EntityManagerInterface $em, Environment $templating) should be enough.

Thus leaving your __construct() method like this:

// you can of course import the EngineInterface with a "use" statement. 
public function __construct(
EntityManagerInterface $em,
Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}

If you do this and remove the bind configuration, automatic dependency injection should work by itself.

(Normally I would suggest replacing Environment with Symfony\Bundle\FrameworkBundle\Templating\EngineInterface, to depend in the interface provided by the framework to integrate with the templating component. But this component and its integration have been deprecated in 4.3, and will be removed by 5.0; so you are fine by depending directly on Twig.)

But if want to you leave the binding in place for some reason, you should prefix the service name with an @ symbol, so Symfony knows you are trying to inject a service and not string. Like so:

 bind:
$em: '@doctrine.orm.default_entity_manager'

Docs.



Related Topics



Leave a reply



Submit