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
+50-63
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
--------------
@@ -16,35 +19,21 @@ First lets define our ``Product`` document:
16
19
17
20
namespace Documents;
18
21
19
-
/** @Document */
22
+
#[Document]
20
23
class Product
21
24
{
22
-
/** @Id */
23
-
private $id;
25
+
#[Id]
26
+
public string $id;
24
27
25
-
/** @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
-
}
28
+
#[Field(type: 'string')]
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
@@ -54,27 +43,19 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
54
43
55
44
use Documents\Product;
56
45
57
-
/**
58
-
* @Entity
59
-
* @Table(name="orders")
60
-
*/
46
+
#[Entity]
47
+
#[Table(name: 'orders')]
61
48
class Order
62
49
{
63
-
/**
64
-
* @Id @Column(type="int")
65
-
* @GeneratedValue(strategy="AUTO")
66
-
*/
67
-
private $id;
68
-
69
-
/**
70
-
* @Column(type="string")
71
-
*/
72
-
private $productId;
73
-
74
-
/**
75
-
* @var Documents\Product
76
-
*/
77
-
private $product;
50
+
#[Id]
51
+
#[Column(type: 'int')]
52
+
#[GeneratedValue(strategy: 'AUTO')]
53
+
public int $id;
54
+
55
+
#[Column(type: 'string')]
56
+
private string $productId;
57
+
58
+
private Product $product;
78
59
79
60
public function getId(): ?int
80
61
{
@@ -101,7 +82,10 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
101
82
Event Subscriber
102
83
----------------
103
84
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:
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:
105
89
106
90
.. code-block:: php
107
91
@@ -112,15 +96,17 @@ Now we need to setup an event subscriber that will set the ``$product`` property
112
96
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
113
97
);
114
98
115
-
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:
124
110
125
111
.. code-block:: php
126
112
@@ -131,10 +117,9 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
131
117
132
118
class MyEventSubscriber
133
119
{
134
-
public function __construct(DocumentManager $dm)
135
-
{
136
-
$this->dm = $dm;
137
-
}
120
+
public function __construct(
121
+
private readonly DocumentManager $dm,
122
+
) {}
138
123
139
124
public function postLoad(LifecycleEventArgs $eventArgs): void
140
125
{
@@ -144,13 +129,13 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
0 commit comments