Skip to content

Commit

Permalink
minor #3545 Example of getting entity managers directly from the cont…
Browse files Browse the repository at this point in the history
…ainer (colinodell)

This PR was merged into the 2.3 branch.

Discussion
----------

Example of getting entity managers directly from the container

The documentation didn't provide examples of how to obtain the entity managers directly from the DI container.  This is useful when needing to inject the em into other services.

The ems are added to the container using the service id `doctrine.orm.%s_entity_manager`, where `%s` is the em's name.  This is done by [`\Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension::loadOrmEntityManager()`](https://github.com/doctrine/DoctrineBundle/blob/v1.0.0/DependencyInjection/DoctrineExtension.php#L342)

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes? (added example of existing feature)
| Applies to    | all (doctrine/doctrine-bundle v1.0.0+)
| Fixed tickets | n/a

Commits
-------

eb7594c Example of getting entity managers directly from the container
  • Loading branch information
weaverryan committed Feb 16, 2014
2 parents 6a2a55b + eb7594c commit ffa8f76
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cookbook/doctrine/multiple_entity_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,14 @@ the default entity manager (i.e. ``default``) is returned::
{
public function indexAction()
{
// both return the "default" em
// All three return the "default" entity manager
$em = $this->get('doctrine')->getManager();
$em = $this->get('doctrine')->getManager('default');
$em = $this->get('doctrine.orm.default_entity_manager');

$customerEm = $this->get('doctrine')->getManager('customer');
// Both of these return the "customer" entity manager
$customerEm = $this->get('doctrine')->getManager('customer');
$customerEm = $this->get('doctrine.orm.customer_entity_manager');
}
}

Expand Down

0 comments on commit ffa8f76

Please sign in to comment.