Skip to content

Improve multiple em howto #1611

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 2 commits into from
Aug 1, 2012
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
38 changes: 35 additions & 3 deletions cookbook/doctrine/multiple_entity_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ and ``customer``. The ``default`` entity manager manages entities in the
manager manages entities in the ``AcmeCustomerBundle``.

When working with multiple entity managers, you should be explicit about which
entity manager you want. If you *do* omit the entity manager's name when
asking for it, the default entity manager (i.e. ``default``) is returned::
entity manager you want. If you *do* omit the entity manager's name when you
update your schema, the default (i.e. ``default``) is used::

# Play only with "default" mappings
php app/console doctrine:schema:update --force

# Play only with "customer" mappings
php app/console doctrine:schema:update --force --em=customer

If you *do* omit the entity manager's name when asking for it,
the default entity manager (i.e. ``default``) is returned::

class UserController extends Controller
{
Expand All @@ -52,11 +61,34 @@ asking for it, the default entity manager (i.e. ``default``) is returned::
// both return the "default" em
$em = $this->get('doctrine')->getEntityManager();
$em = $this->get('doctrine')->getEntityManager('default');

$customerEm = $this->get('doctrine')->getEntityManager('customer');
}
}

You can now use Doctrine just as you did before - using the ``default`` entity
manager to persist and fetch entities that it manages and the ``customer``
entity manager to persist and fetch its entities.

The same applies to repository call::

class UserController extends Controller
{
public function indexAction()
{
// Retrieves a repository managed by the "default" em
$products = $this->get('doctrine')
->getRepository('AcmeStoreBundle:Product')
->findAll();

// Explicit way to deal with the "default" em
$products = $this->get('doctrine')
->getRepository('AcmeStoreBundle:Product', 'default')
->findAll();

// Retrieves a repository managed by the "customer" em
$customers = $this->get('doctrine')
->getRepository('AcmeCustomerBundle:Customer', 'customer')
->findAll();
}
}