Skip to content

Commit e849c5d

Browse files
committed
Merge pull request #1611 from kipit/improve_multiple_em_howto
Improve multiple em howto
2 parents a0607d1 + 8b54ed7 commit e849c5d

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

cookbook/doctrine/multiple_entity_managers.rst

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ and ``customer``. The ``default`` entity manager manages entities in the
4242
manager manages entities in the ``AcmeCustomerBundle``.
4343

4444
When working with multiple entity managers, you should be explicit about which
45-
entity manager you want. If you *do* omit the entity manager's name when
46-
asking for it, the default entity manager (i.e. ``default``) is returned::
45+
entity manager you want. If you *do* omit the entity manager's name when you
46+
update your schema, the default (i.e. ``default``) is used::
47+
48+
# Play only with "default" mappings
49+
php app/console doctrine:schema:update --force
50+
51+
# Play only with "customer" mappings
52+
php app/console doctrine:schema:update --force --em=customer
53+
54+
If you *do* omit the entity manager's name when asking for it,
55+
the default entity manager (i.e. ``default``) is returned::
4756

4857
class UserController extends Controller
4958
{
@@ -52,11 +61,34 @@ asking for it, the default entity manager (i.e. ``default``) is returned::
5261
// both return the "default" em
5362
$em = $this->get('doctrine')->getEntityManager();
5463
$em = $this->get('doctrine')->getEntityManager('default');
55-
64+
5665
$customerEm = $this->get('doctrine')->getEntityManager('customer');
5766
}
5867
}
5968

6069
You can now use Doctrine just as you did before - using the ``default`` entity
6170
manager to persist and fetch entities that it manages and the ``customer``
6271
entity manager to persist and fetch its entities.
72+
73+
The same applies to repository call::
74+
75+
class UserController extends Controller
76+
{
77+
public function indexAction()
78+
{
79+
// Retrieves a repository managed by the "default" em
80+
$products = $this->get('doctrine')
81+
->getRepository('AcmeStoreBundle:Product')
82+
->findAll();
83+
84+
// Explicit way to deal with the "default" em
85+
$products = $this->get('doctrine')
86+
->getRepository('AcmeStoreBundle:Product', 'default')
87+
->findAll();
88+
89+
// Retrieves a repository managed by the "customer" em
90+
$customers = $this->get('doctrine')
91+
->getRepository('AcmeCustomerBundle:Customer', 'customer')
92+
->findAll();
93+
}
94+
}

0 commit comments

Comments
 (0)