You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/cookbook/blending-orm-and-mongodb-odm.rst
+28-11Lines changed: 28 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,12 @@ Blending the ORM and MongoDB ODM
3
3
4
4
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.
5
5
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.
7
7
8
8
Define Product
9
9
--------------
10
10
11
-
First lets define our `Product` document:
11
+
First lets define our ``Product`` document:
12
12
13
13
.. code-block:: php
14
14
@@ -44,7 +44,7 @@ First lets define our `Product` document:
44
44
Define Entity
45
45
-------------
46
46
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:
48
48
49
49
.. code-block:: php
50
50
@@ -101,7 +101,7 @@ Next create the `Order` entity that has a `$product` and `$productId` property l
101
101
Event Subscriber
102
102
----------------
103
103
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:
105
105
106
106
.. code-block:: php
107
107
@@ -112,7 +112,15 @@ Now we need to setup an event subscriber that will set the `$product` property o
112
112
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
113
113
);
114
114
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:
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:
116
124
117
125
.. code-block:: php
118
126
@@ -131,22 +139,31 @@ So now we need to define a class named `MyEventSubscriber` and pass a dependency
131
139
public function postLoad(LifecycleEventArgs $eventArgs): void
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.
145
162
146
163
Working with Products and Orders
147
164
--------------------------------
148
165
149
-
First create a new `Product`:
166
+
First create a new ``Product``:
150
167
151
168
.. code-block:: php
152
169
@@ -157,7 +174,7 @@ First create a new `Product`:
157
174
$dm->persist($product);
158
175
$dm->flush();
159
176
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:
161
178
162
179
.. code-block:: php
163
180
@@ -180,7 +197,7 @@ Later we can retrieve the entity and lazily load the reference to the document i
180
197
181
198
echo "Order Title: " . $product->getTitle();
182
199
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:
0 commit comments