Skip to content

Commit acb7cf9

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Reword Add Doctrine field types reference page
2 parents 8b25c45 + aacbfca commit acb7cf9

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

components/uid.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ The following constants are available:
305305
You can also use the ``Uuid::FORMAT_ALL`` constant to accept any UUID format.
306306
By default, only the RFC 4122 format is accepted.
307307

308+
.. _uid-uuid-doctrine:
309+
308310
Storing UUIDs in Databases
309311
~~~~~~~~~~~~~~~~~~~~~~~~~~
310312

@@ -565,6 +567,8 @@ ULID objects created with the ``Ulid`` class can use the following methods::
565567
// this method returns $ulid1 <=> $ulid2
566568
$ulid1->compare($ulid2); // e.g. int(-1)
567569

570+
.. _uid-ulid-doctrine:
571+
568572
Storing ULIDs in Databases
569573
~~~~~~~~~~~~~~~~~~~~~~~~~~
570574

doctrine.rst

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ the ``#[ORM\Column(...)]`` comments that you see above each property:
222222
The ``make:entity`` command is a tool to make life easier. But this is *your* code:
223223
add/remove fields, add/remove methods or update configuration.
224224

225-
Doctrine supports a wide variety of field types, each with their own options.
226-
Check out the `list of Doctrine mapping types`_ in the Doctrine documentation.
227-
If you want to use XML instead of attributes, add ``type: xml`` and
228-
``dir: '%kernel.project_dir%/config/doctrine'`` to the entity mappings in your
229-
``config/packages/doctrine.yaml`` file.
230-
231225
.. warning::
232226

233227
Be careful not to use reserved SQL keywords as your table or column names
@@ -236,6 +230,118 @@ If you want to use XML instead of attributes, add ``type: xml`` and
236230
``#[ORM\Table(name: 'groups')]`` above the class or configure the column name with
237231
the ``name: 'group_name'`` option.
238232

233+
Entity Field Types
234+
~~~~~~~~~~~~~~~~~~
235+
236+
Doctrine supports a wide variety of **field types** (numbers, strings, enums,
237+
binary, dates, JSON, etc.), each with their own options. Check out the
238+
`list of Doctrine mapping types`_ in the Doctrine documentation.
239+
240+
Symfony also provides the following **additional field types**:
241+
242+
``uuid``
243+
........
244+
245+
**Class:** :class:`Symfony\\Bridge\\Doctrine\\Types\\UuidType`
246+
247+
Stores a :doc:`UUID </components/uid>` as a native GUID type if available, or
248+
as a 16-byte binary otherwise::
249+
250+
// src/Entity/Product.php
251+
namespace App\Entity;
252+
253+
use Doctrine\ORM\Mapping as ORM;
254+
use Symfony\Bridge\Doctrine\Types\UuidType;
255+
use Symfony\Component\Uid\Uuid;
256+
257+
#[ORM\Entity]
258+
class Product
259+
{
260+
#[ORM\Column(type: UuidType::NAME)]
261+
private Uuid $sku;
262+
263+
// ...
264+
}
265+
266+
See :ref:`Storing UUIDs in Databases <uid-uuid-doctrine>` in the UID component
267+
documentation for more details, including how to use UUIDs as primary keys.
268+
269+
``ulid``
270+
........
271+
272+
**Class:** :class:`Symfony\\Bridge\\Doctrine\\Types\\UlidType`
273+
274+
Stores a :ref:`ULID <ulid>` as a native GUID type if available, or as a
275+
16-byte binary otherwise::
276+
277+
// src/Entity/Product.php
278+
namespace App\Entity;
279+
280+
use Doctrine\ORM\Mapping as ORM;
281+
use Symfony\Bridge\Doctrine\Types\UlidType;
282+
use Symfony\Component\Uid\Ulid;
283+
284+
#[ORM\Entity]
285+
class Product
286+
{
287+
#[ORM\Column(type: UlidType::NAME)]
288+
private Ulid $identifier;
289+
290+
// ...
291+
}
292+
293+
See :ref:`Storing ULIDs in Databases <uid-ulid-doctrine>` in the UID component
294+
documentation for more details, including how to use ULIDs as primary keys.
295+
296+
DatePoint Types
297+
...............
298+
299+
These types allow storing :class:`Symfony\\Component\\Clock\\DatePoint` objects
300+
from the :doc:`Clock component </components/clock>`. They convert to/from
301+
``DatePoint`` objects automatically.
302+
303+
==================== ========================== =========================================================
304+
Type Extends Doctrine type Class
305+
==================== ========================== =========================================================
306+
``date_point`` ``datetime_immutable`` :class:`Symfony\\Bridge\\Doctrine\\Types\\DatePointType`
307+
``day_point`` ``date_immutable`` :class:`Symfony\\Bridge\\Doctrine\\Types\\DayPointType`
308+
``time_point`` ``time_immutable`` :class:`Symfony\\Bridge\\Doctrine\\Types\\TimePointType`
309+
==================== ========================== =========================================================
310+
311+
Example usage::
312+
313+
// src/Entity/Product.php
314+
namespace App\Entity;
315+
316+
use Doctrine\ORM\Mapping as ORM;
317+
use Symfony\Component\Clock\DatePoint;
318+
319+
#[ORM\Entity]
320+
class Product
321+
{
322+
// Symfony autodetects the 'date_point' type when type-hinting with DatePoint
323+
#[ORM\Column]
324+
private DatePoint $createdAt;
325+
326+
// you can also set the type explicitly
327+
#[ORM\Column(type: 'date_point')]
328+
private DatePoint $updatedAt;
329+
330+
#[ORM\Column(type: 'day_point')]
331+
public DatePoint $releaseDate;
332+
333+
#[ORM\Column(type: 'time_point')]
334+
public DatePoint $openingTime;
335+
336+
// ...
337+
}
338+
339+
.. tip::
340+
341+
Use ``date_point`` when you want to work with :class:`Symfony\\Component\\Clock\\DatePoint`
342+
objects, which makes your code easier to test with the :doc:`Clock component </components/clock>`.
343+
Use ``datetime_immutable`` if you don't need the Clock component features.
344+
239345
.. _doctrine-creating-the-database-tables-schema:
240346

241347
Migrations: Creating the Database Tables/Schema

0 commit comments

Comments
 (0)