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
+39-47Lines changed: 39 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,12 @@
1
1
Blending the ORM and MongoDB ODM
2
2
================================
3
3
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.
4
+
This article demonstrates how you can integrate the `Doctrine MongoDB ODM`_
5
+
and the `ORM`_ transparently, maintaining a clean domain model. This is
6
+
something that is supported indirectly by the libraries by using the events.
5
7
6
-
This example will have a ``Product`` that is stored in MongoDB and the ``Order`` stored in a MySQL database.
8
+
This example will have a ``Product`` that is stored in MongoDB and the ``Order``
9
+
stored in a SQL database like MySQL, PostgreSQL or SQLite.
7
10
8
11
Define Product
9
12
--------------
@@ -20,31 +23,17 @@ First lets define our ``Product`` document:
20
23
class Product
21
24
{
22
25
#[Id]
23
-
private $id;
26
+
public string $id;
24
27
25
28
#[Field(type: 'string')]
26
-
private $title;
27
-
28
-
public function getId(): ?string
29
-
{
30
-
return $this->id;
31
-
}
32
-
33
-
public function getTitle(): ?string
34
-
{
35
-
return $this->title;
36
-
}
37
-
38
-
public function setTitle(string $title): void
39
-
{
40
-
$this->title = $title;
41
-
}
29
+
public string $title;
42
30
}
43
31
44
32
Define Entity
45
33
-------------
46
34
47
-
Next create the ``Order`` entity that has a ``$product`` and ``$productId`` property linking it to the ``Product`` that is stored with MongoDB:
35
+
Next create the ``Order`` entity that has a ``$product`` and ``$productId``
36
+
property linking it to the ``Product`` that is stored with MongoDB:
48
37
49
38
.. code-block:: php
50
39
@@ -61,15 +50,12 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
61
50
#[Id]
62
51
#[Column(type: 'int')]
63
52
#[GeneratedValue(strategy: 'AUTO')]
64
-
private $id;
53
+
public int $id;
65
54
66
55
#[Column(type: 'string')]
67
-
private $productId;
56
+
private string $productId;
68
57
69
-
/**
70
-
* @var Documents\Product
71
-
*/
72
-
private $product;
58
+
private Product $product;
73
59
74
60
public function getId(): ?int
75
61
{
@@ -96,7 +82,10 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
96
82
Event Subscriber
97
83
----------------
98
84
99
-
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:
85
+
Now we need to setup an event subscriber that will set the ``$product`` property
86
+
of all ``Order`` instances to a reference to the document product so it can be
87
+
lazily loaded when it is accessed the first time. So first register a new event
88
+
subscriber:
100
89
101
90
.. code-block:: php
102
91
@@ -107,15 +96,17 @@ Now we need to setup an event subscriber that will set the ``$product`` property
107
96
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
108
97
);
109
98
110
-
or in .yaml
99
+
or in YAML configuration of the Symfony container:
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:
107
+
So now we need to define a class named ``MyEventSubscriber`` and pass
108
+
``DocumentManager`` as a dependency. It will have a ``postLoad()`` method that
109
+
sets the product document reference:
119
110
120
111
.. code-block:: php
121
112
@@ -126,10 +117,9 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
126
117
127
118
class MyEventSubscriber
128
119
{
129
-
public function __construct(DocumentManager $dm)
130
-
{
131
-
$this->dm = $dm;
132
-
}
120
+
public function __construct(
121
+
private readonly DocumentManager $dm,
122
+
) {}
133
123
134
124
public function postLoad(LifecycleEventArgs $eventArgs): void
135
125
{
@@ -139,13 +129,13 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
0 commit comments