Skip to content

Commit 39c5285

Browse files
authored
Merge pull request #2677 from doctrine/2.9.x-merge-up-into-3.0.x_f7Yvsmfj
Merge release 2.9.0 into 3.0.x
2 parents 296b025 + 3c9b1e8 commit 39c5285

File tree

78 files changed

+1573
-781
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1573
-781
lines changed

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ updates:
44
directory: "/"
55
schedule:
66
interval: "weekly"
7+
labels:
8+
- "CI"

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"ext-bcmath": "*",
4141
"doctrine/annotations": "^1.12 || ^2.0",
4242
"doctrine/coding-standard": "^12.0",
43+
"doctrine/orm": "^3.2",
4344
"jmikola/geojson": "^1.0",
4445
"phpbench/phpbench": "^1.0.0",
4546
"phpstan/phpstan": "~1.10.67",

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

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
Blending the ORM and MongoDB ODM
22
================================
33

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.
57

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.
710

811
Define Product
912
--------------
@@ -20,31 +23,17 @@ First lets define our ``Product`` document:
2023
class Product
2124
{
2225
#[Id]
23-
private $id;
26+
public string $id;
2427
2528
#[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;
4230
}
4331
4432
Define Entity
4533
-------------
4634

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:
4837

4938
.. code-block:: php
5039
@@ -61,15 +50,12 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
6150
#[Id]
6251
#[Column(type: 'int')]
6352
#[GeneratedValue(strategy: 'AUTO')]
64-
private $id;
53+
public int $id;
6554
6655
#[Column(type: 'string')]
67-
private $productId;
56+
private string $productId;
6857
69-
/**
70-
* @var Documents\Product
71-
*/
72-
private $product;
58+
private Product $product;
7359
7460
public function getId(): ?int
7561
{
@@ -96,7 +82,10 @@ Next create the ``Order`` entity that has a ``$product`` and ``$productId`` prop
9682
Event Subscriber
9783
----------------
9884

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:
10089

10190
.. code-block:: php
10291
@@ -107,15 +96,17 @@ Now we need to setup an event subscriber that will set the ``$product`` property
10796
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
10897
);
10998
110-
or in .yaml
99+
or in YAML configuration of the Symfony container:
111100

112101
.. code-block:: yaml
113102
114103
App\Listeners\MyEventSubscriber:
115104
tags:
116-
- { name: doctrine.event_listener, connection: default, event: postLoad}
105+
- { name: doctrine.event_listener, connection: default, event: postLoad }
117106
118-
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:
119110

120111
.. code-block:: php
121112
@@ -126,10 +117,9 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
126117
127118
class MyEventSubscriber
128119
{
129-
public function __construct(DocumentManager $dm)
130-
{
131-
$this->dm = $dm;
132-
}
120+
public function __construct(
121+
private readonly DocumentManager $dm,
122+
) {}
133123
134124
public function postLoad(LifecycleEventArgs $eventArgs): void
135125
{
@@ -139,13 +129,13 @@ So now we need to define a class named ``MyEventSubscriber`` and pass ``Document
139129
return;
140130
}
141131
142-
$em = $eventArgs->getEntityManager();
143-
$productReflProp = $em->getClassMetadata(Order::class)
144-
->reflClass->getProperty('product');
145-
$productReflProp->setAccessible(true);
146-
$productReflProp->setValue(
147-
$order, $this->dm->getReference(Product::class, $order->getProductId())
148-
);
132+
$product = $this->dm->getReference(Product::class, $order->getProductId());
133+
134+
$eventArgs->getObjectManager()
135+
->getClassMetadata(Order::class)
136+
->reflClass
137+
->getProperty('product')
138+
->setValue($order, $product);
149139
}
150140
}
151141
@@ -165,7 +155,7 @@ First create a new ``Product``:
165155
<?php
166156
167157
$product = new \Documents\Product();
168-
$product->setTitle('Test Product');
158+
$product->title = 'Test Product';
169159
$dm->persist($product);
170160
$dm->flush();
171161
@@ -180,19 +170,21 @@ Now create a new ``Order`` and link it to a ``Product`` in MySQL:
180170
$em->persist($order);
181171
$em->flush();
182172
183-
Later we can retrieve the entity and lazily load the reference to the document in MongoDB:
173+
Later we can retrieve the entity and lazily load the reference to the document
174+
in MongoDB:
184175

185176
.. code-block:: php
186177
187178
<?php
188179
189-
$order = $em->find(Order::class, $order->getId());
180+
$order = $em->find(Order::class, $order->id);
190181
191182
$product = $order->getProduct();
192183
193-
echo "Order Title: " . $product->getTitle();
184+
echo "Order Title: " . $product->title;
194185
195-
If you were to print the ``$order`` you would see that we got back regular PHP objects:
186+
If you were to print the ``$order`` you would see that we got back regular PHP
187+
objects:
196188

197189
.. code-block:: php
198190
@@ -216,5 +208,5 @@ The above would output the following:
216208
)
217209
)
218210
219-
.. _Doctrine MongoDB Object Document Mapper: http://www.doctrine-project.org/projects/mongodb_odm
211+
.. _Doctrine MongoDB ODM: http://www.doctrine-project.org/projects/mongodb_odm
220212
.. _ORM: http://www.doctrine-project.org/projects/orm

docs/en/cookbook/implementing-wakeup-or-clone.rst

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)