@@ -34,8 +34,8 @@ annotations by offering a standardized approach to metadata, eliminating
3434the need for the separate parsing library required by annotations.
3535
3636In 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.
3939
4040.. note ::
4141
@@ -109,8 +109,8 @@ option as follows:
109109 Now instances of ``Documents\User `` will be persisted into a
110110collection named ``users `` in the database ``my_db ``.
111111
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:
114114
115115.. code-block :: php
116116
@@ -195,7 +195,7 @@ _________________
195195 Doctrine will skip type autoconfiguration if settings are provided explicitly.
196196
197197Since 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
199199follows:
200200
201201- ``DateTime ``: ``date ``
@@ -392,12 +392,9 @@ Then specify the ``class`` option for the ``CUSTOM`` strategy:
392392 Fields
393393~~~~~~
394394
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.
401398
402399Example:
403400
@@ -417,7 +414,7 @@ Example:
417414 {
418415 // ...
419416
420- #[Field(type: 'string') ]
417+ #[Field]
421418 public string $username;
422419 }
423420
@@ -436,11 +433,9 @@ Example:
436433
437434 In that example we mapped the property ``id `` to the field ``id ``
438435using 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:
444439
445440.. configuration-block ::
446441
@@ -458,6 +453,46 @@ as follows:
458453
459454 <field field-name =" name" name =" db_name" />
460455
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+
461496Multiple Document Types in a Collection
462497---------------------------------------
463498
0 commit comments