@@ -34,8 +34,8 @@ annotations by offering a standardized approach to metadata, eliminating
34
34
the need for the separate parsing library required by annotations.
35
35
36
36
In this documentation we follow the `PER Coding Style <https://www.php-fig.org/per/coding-style/#12-attributes >`_
37
- for attributes. We use named arguments for attributes as they argument names
38
- or attribute classes constructors are covered by Doctrine Backward-Compatibility promise.
37
+ for attributes. We use named arguments for attributes as the names of their
38
+ constructor arguments are covered by Doctrine Backward-Compatibility promise.
39
39
40
40
.. note ::
41
41
@@ -109,8 +109,8 @@ option as follows:
109
109
Now instances of ``Documents\User `` will be persisted into a
110
110
collection named ``users `` in the database ``my_db ``.
111
111
112
- If you want to omit the db attribute you can configure the default db
113
- to use with the ``setDefaultDB `` method:
112
+ If you want to omit the `` db `` argument you can configure the default database
113
+ to use with the ``setDefaultDB() `` method:
114
114
115
115
.. code-block :: php
116
116
@@ -195,7 +195,7 @@ _________________
195
195
Doctrine will skip type autoconfiguration if settings are provided explicitly.
196
196
197
197
Since version 2.4 Doctrine can determine usable defaults from property types
198
- on document classes. Doctrine will map PHP types to ``type `` attribute as
198
+ on document classes. Doctrine will map PHP types to ``type `` arguments as
199
199
follows:
200
200
201
201
- ``DateTime ``: ``date ``
@@ -392,12 +392,9 @@ Then specify the ``class`` option for the ``CUSTOM`` strategy:
392
392
Fields
393
393
~~~~~~
394
394
395
- To mark a property for document persistence the ``#[Field] `` docblock
396
- attribute can be used. This attribute usually requires at least 1
397
- attribute to be set, the ``type ``. The ``type `` attribute specifies
398
- the Doctrine Mapping Type to use for the field. If the type is not
399
- specified, 'string' is used as the default mapping type since it is
400
- the most flexible.
395
+ To mark a property to be persisted in the document, add the ``#[Field] ``
396
+ attribute. The name and the type of the field are inferred from the property
397
+ name and type.
401
398
402
399
Example:
403
400
@@ -417,7 +414,7 @@ Example:
417
414
{
418
415
// ...
419
416
420
- #[Field(type: 'string') ]
417
+ #[Field]
421
418
public string $username;
422
419
}
423
420
@@ -436,11 +433,9 @@ Example:
436
433
437
434
In that example we mapped the property ``id `` to the field ``id ``
438
435
using the mapping type ``id `` and the property ``name `` is mapped
439
- to the field ``name `` with the default mapping type ``string ``. As
440
- you can see, by default the mongo field names are assumed to be the
441
- same as the property names. To specify a different name for the
442
- field, you can use the ``name `` attribute of the Field attribute
443
- as follows:
436
+ to the field ``name `` with the default mapping type ``string ``.
437
+ To specify a different name for the field, you can use the ``name `` argument
438
+ of the Field attribute as follows:
444
439
445
440
.. configuration-block ::
446
441
@@ -458,6 +453,46 @@ as follows:
458
453
459
454
<field field-name =" name" name =" db_name" />
460
455
456
+ Date Fields
457
+ ~~~~~~~~~~~
458
+
459
+ MongoDB has a specific database type to store date and time values. You should never
460
+ use a string to store a date. To define a date field, use the property types
461
+ ``DateTime `` or ``DateTimeImmutable `` so that Doctrine maps it to a BSON date.
462
+ The ``date `` and ``date_immutable `` mapping types can be used explicitly;
463
+ they are required only if the property type is ``DateTimeInterface `` or not set.
464
+
465
+ .. configuration-block ::
466
+
467
+ .. code-block :: php
468
+
469
+ <?php
470
+
471
+ class User
472
+ {
473
+ #[Field]
474
+ public \DateTimeImmutable $immutableDateTime;
475
+
476
+ #[Field]
477
+ public \DateTime $mutableDateTime;
478
+
479
+ #[Field(type: 'date_immutable')]
480
+ public \DateTimeInterface $datetime;
481
+ }
482
+
483
+ .. code-block :: xml
484
+
485
+ <field field-name =" immutableDateTime" type =" date_immutable" />
486
+ <field field-name =" mutableDateTime" name =" date" />
487
+ <field field-name =" datetime" name =" date_immutable" />
488
+
489
+
490
+ .. note ::
491
+
492
+ Prefer using ``DateTimeImmutable `` over ``DateTime `` to avoid issues with
493
+ mutable objects. If you need to modify a date, create a new instance
494
+ and assign it to the property.
495
+
461
496
Multiple Document Types in a Collection
462
497
---------------------------------------
463
498
0 commit comments