Skip to content

Commit 608d94f

Browse files
authored
Merge pull request #2151 from doctrine/1.3.x-merge-up-into-2.0.x_5e1cb820449e20.98695361
Merge release 1.3.6 into 2.0.x
2 parents 9bb6593 + 11243e3 commit 608d94f

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

docs/en/cookbook/blending-orm-and-mongodb-odm.rst

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Blending the ORM and MongoDB ODM
33

44
Since the start of the `Doctrine MongoDB Object Document Mapper`_ project people have asked how it can be integrated with the `ORM`_. This article will demonstrates how you can integrate the two transparently, maintaining a clean domain model.
55

6-
This example will have a `Product` that is stored in MongoDB and the `Order` stored in a MySQL database.
6+
This example will have a ``Product`` that is stored in MongoDB and the ``Order`` stored in a MySQL database.
77

88
Define Product
99
--------------
1010

11-
First lets define our `Product` document:
11+
First lets define our ``Product`` document:
1212

1313
.. code-block:: php
1414
@@ -44,7 +44,7 @@ First lets define our `Product` document:
4444
Define Entity
4545
-------------
4646

47-
Next create the `Order` entity that has a `$product` and `$productId` property linking it to the `Product` that is stored with MongoDB:
47+
Next create the ``Order`` entity that has a ``$product`` and ``$productId`` property linking it to the ``Product`` that is stored with MongoDB:
4848

4949
.. code-block:: php
5050
@@ -101,7 +101,7 @@ Next create the `Order` entity that has a `$product` and `$productId` property l
101101
Event Subscriber
102102
----------------
103103

104-
Now we need to setup an event subscriber that will set the `$product` property of all `Order` instances to a reference to the document product so it can be lazily loaded when it is accessed the first time. So first register a new event subscriber:
104+
Now we need to setup an event subscriber that will set the ``$product`` property of all ``Order`` instances to a reference to the document product so it can be lazily loaded when it is accessed the first time. So first register a new event subscriber:
105105

106106
.. code-block:: php
107107
@@ -112,7 +112,15 @@ Now we need to setup an event subscriber that will set the `$product` property o
112112
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
113113
);
114114
115-
So now we need to define a class named `MyEventSubscriber` and pass a dependency to the `DocumentManager`. It will have a `postLoad()` method that sets the product document reference:
115+
or in .yaml
116+
117+
.. code-block:: yaml
118+
119+
App\Listeners\MyEventSubscriber:
120+
tags:
121+
- { name: doctrine.event_listener, connection: default, event: postLoad}
122+
123+
So now we need to define a class named ``MyEventSubscriber`` and pass ``DocumentManager`` as a dependency. It will have a ``postLoad()`` method that sets the product document reference:
116124

117125
.. code-block:: php
118126
@@ -131,22 +139,31 @@ So now we need to define a class named `MyEventSubscriber` and pass a dependency
131139
public function postLoad(LifecycleEventArgs $eventArgs): void
132140
{
133141
$order = $eventArgs->getEntity();
142+
143+
if (!$order instanceof Order) {
144+
return;
145+
}
146+
134147
$em = $eventArgs->getEntityManager();
135-
$productReflProp = $em->getClassMetadata('Entities\Order')
148+
$productReflProp = $em->getClassMetadata(Order::class)
136149
->reflClass->getProperty('product');
137150
$productReflProp->setAccessible(true);
138151
$productReflProp->setValue(
139-
$order, $this->dm->getReference('Documents\Product', $order->getProductId())
152+
$order, $this->dm->getReference(Product::class, $order->getProductId())
140153
);
141154
}
142155
}
143156
144-
The `postLoad` method will be invoked after an ORM entity is loaded from the database. This allows us to use the `DocumentManager` to set the `$product` property with a reference to the `Product` document with the product id we previously stored.
157+
The ``postLoad`` method will be invoked after an ORM entity is loaded from the database. This allows us
158+
to use the ``DocumentManager`` to set the ``$product`` property with a reference to the ``Product`` document
159+
with the product id we previously stored. Please note, that the event subscriber will be called on
160+
postLoad for all entities that are loaded by doctrine. Thus, it is recommended to check for the current
161+
entity.
145162

146163
Working with Products and Orders
147164
--------------------------------
148165

149-
First create a new `Product`:
166+
First create a new ``Product``:
150167

151168
.. code-block:: php
152169
@@ -157,7 +174,7 @@ First create a new `Product`:
157174
$dm->persist($product);
158175
$dm->flush();
159176
160-
Now create a new `Order` and link it to a `Product` in MySQL:
177+
Now create a new ``Order`` and link it to a ``Product`` in MySQL:
161178

162179
.. code-block:: php
163180
@@ -180,7 +197,7 @@ Later we can retrieve the entity and lazily load the reference to the document i
180197
181198
echo "Order Title: " . $product->getTitle();
182199
183-
If you were to print the `$order` you would see that we got back regular PHP objects:
200+
If you were to print the ``$order`` you would see that we got back regular PHP objects:
184201

185202
.. code-block:: php
186203

0 commit comments

Comments
 (0)