Skip to content

Example of getting entity managers directly from the container #3545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()` (see: https://github.com/doctrine/DoctrineBundle/blob/v1.2.0/DependencyInjection/DoctrineExtension.php#L347)
  • Loading branch information
colinodell committed Feb 5, 2014
commit eb7594c308e6d69b247bdcf724c724863f5ffdcb
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