types.rst 65 KB
Newer Older
1 2 3 4 5 6 7
Types
=====

Besides abstraction of SQL one needs a translation between database
and PHP data-types to implement database independent applications.
Doctrine 2 has a type translation system baked in that supports the
conversion from and to PHP values from any database platform,
8
as well as platform independent SQL generation for any Doctrine
9 10 11
Type.

Using the ORM you generally don't need to know about the Type
Andreas Fischer's avatar
Andreas Fischer committed
12
system. This is unless you want to make use of database vendor
13
specific database types not included in Doctrine 2.
14 15 16 17 18 19

Types are flyweights. This means there is only ever one instance of
a type and it is not allowed to contain any state. Creation of type
instances is abstracted through a static get method
``Doctrine\DBAL\Types\Type::getType()``.

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
Types are abstracted across all the supported database
vendors.

Reference
---------

The following chapter gives an overview of all available Doctrine 2
types with short explanations on their context and usage.
The type names listed here equal those that can be passed to the
``Doctrine\DBAL\Types\Type::getType()``  factory method in order to retrieve
the desired type instance.

.. code-block:: php

    <?php

    // Returns instance of \Doctrine\DBAL\Types\IntegerType
    $type = \Doctrine\DBAL\Types\Type::getType('integer');

Numeric types
~~~~~~~~~~~~~

Types that map numeric data such as integers, fixed and floating point
numbers.

Integer types
^^^^^^^^^^^^^

Types that map numeric data without fractions.

smallint
++++++++

Maps and converts 2-byte integer values.
Unsigned integer values have a range of **0** to **65535** while signed
integer values have a range of **−32768** to **32767**.
If you know the integer data you want to store always fits into one of these ranges
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``integer`` type
or ``null`` if no data is present.

.. note::

    Not all of the database vendors support unsigned integers, so such an assumption
    might not be propagated to the database.

integer
+++++++

Maps and converts 4-byte integer values.
Unsigned integer values have a range of **0** to **4294967295** while signed
integer values have a range of **−2147483648** to **2147483647**.
If you know the integer data you want to store always fits into one of these ranges
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``integer`` type
or ``null`` if no data is present.

.. note::

    Not all of the database vendors support unsigned integers, so such an assumption
    might not be propagated to the database.

bigint
++++++

Maps and converts 8-byte integer values.
Unsigned integer values have a range of **0** to **18446744073709551615** while signed
integer values have a range of **−9223372036854775808** to **9223372036854775807**.
If you know the integer data you want to store always fits into one of these ranges
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``string`` type
or ``null`` if no data is present.

.. note::

    For compatibility reasons this type is not converted to an integer
    as PHP can only represent big integer values as real integers on
    systems with a 64-bit architecture and would fall back to approximated
    float values otherwise which could lead to false assumptions in applications.

    Not all of the database vendors support unsigned integers, so such an assumption
    might not be propagated to the database.

Decimal types
^^^^^^^^^^^^^

Types that map numeric data with fractions.

decimal
+++++++

Maps and converts numeric data with fixed-point precision.
If you need an exact precision for numbers with fractions, you should consider using
this type.
Values retrieved from the database are always converted to PHP's ``string`` type
or ``null`` if no data is present.

.. note::

    For compatibility reasons this type is not converted to a double
    as PHP can only preserve the precision to a certain degree. Otherwise
    it approximates precision which can lead to false assumptions in
    applications.

float
+++++

Maps and converts numeric data with floating-point precision.
If you only need an approximate precision for numbers with fractions, you should
consider using this type.
Values retrieved from the database are always converted to PHP's
``float``/``double`` type or ``null`` if no data is present.

String types
~~~~~~~~~~~~

Types that map string data such as character and binary text.

Character string types
^^^^^^^^^^^^^^^^^^^^^^

Types that map string data of letters, numbers, and other symbols.

string
++++++

Maps and converts string data with a maximum length.
If you know that the data to be stored always fits into the specified length,
you should consider using this type.
Values retrieved from the database are always converted to PHP's string type
or ``null`` if no data is present.

.. note::

    Database vendors have different limits for the maximum length of a
    varying string. Doctrine internally maps the ``string`` type to the
    vendor's ``text`` type if the maximum allowed length is exceeded.
    This can lead to type inconsistencies when reverse engineering the
    type from the database.

text
++++

Maps and converts string data without a maximum length.
If you don't know the maximum length of the data to be stored, you should
consider using this type.
Values retrieved from the database are always converted to PHP's ``string`` type
or ``null`` if no data is present.

guid
++++

Maps and converts a "Globally Unique Identifier".
If you want to store a GUID, you should consider using this type, as some
database vendors have a native data type for this kind of data which offers
the most efficient way to store it. For vendors that do not support this
type natively, this type is mapped to the ``string`` type internally.
Values retrieved from the database are always converted to PHP's ``string`` type
or ``null`` if no data is present.

Binary string types
^^^^^^^^^^^^^^^^^^^

Types that map binary string data including images and other types of
information that are not interpreted by the database.
If you know that the data to be stored always is in binary format, you
should consider using one of these types in favour of character string
types, as it offers the most efficient way to store it.

binary
++++++

Maps and converts binary string data with a maximum length.
If you know that the data to be stored always fits into the specified length,
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``resource`` type
or ``null`` if no data is present.

.. note::

    Database vendors have different limits for the maximum length of a
    varying binary string. Doctrine internally maps the ``binary`` type to the
    vendor's ``blob`` type if the maximum allowed length is exceeded.
    This can lead to type inconsistencies when reverse engineering the
    type from the database.

blob
++++

Maps and converts binary string data without a maximum length.
If you don't know the maximum length of the data to be stored, you should
consider using this type.
Values retrieved from the database are always converted to PHP's ``resource`` type
or ``null`` if no data is present.

Bit types
~~~~~~~~~

Types that map bit data such as boolean values.

boolean
^^^^^^^

Maps and converts boolean data.
If you know that the data to be stored always is a ``boolean`` (``true`` or ``false``),
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``boolean`` type
or ``null`` if no data is present.

.. note::

    As most of the database vendors do not have a native boolean type,
    this type silently falls back to the smallest possible integer or
    bit data type if necessary to ensure the least possible data storage
    requirements are met.

Date and time types
~~~~~~~~~~~~~~~~~~~

Types that map date, time and timezone related values such as date only,
date and time, date, time and timezone or time only.

date
^^^^

Maps and converts date data without time and timezone information.
If you know that the data to be stored always only needs to be a date
without time and timezone information, you should consider using this type.
Values retrieved from the database are always converted to PHP's ``\DateTime`` object
or ``null`` if no data is present.

251 252 253 254 255 256 257 258 259 260 261
date_immutable
^^^^^^^^^^^^^^

The immutable variant of the ``date`` type.
Values retrieved from the database are always converted to PHP's ``\DateTimeImmutable``
object or ``null`` if no data is present.

.. note::

    Available since version ``2.6``.

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
datetime
^^^^^^^^

Maps and converts date and time data without timezone information.
If you know that the data to be stored always only needs to be a date
with time but without timezone information, you should consider using this type.
Values retrieved from the database are always converted to PHP's ``\DateTime`` object
or ``null`` if no data is present.

.. warning::

    Before 2.5 this type always required a specific format,
    defined in ``$platform->getDateTimeFormatString()``, which
    could cause quite some troubles on platforms that had various
    microtime precision formats.
    Starting with 2.5 whenever the parsing of a date fails with
    the predefined platform format, the ``date_create()``
    function will be used to parse the date.

    This could cause some troubles when your date format is weird
    and not parsed correctly by ``date_create()``, however since
    databases are rather strict on dates there should be no problem.

285 286 287 288 289 290 291 292 293 294 295
datetime_immutable
^^^^^^^^^^^^^^^^^^

The immutable variant of the ``datetime`` type.
Values retrieved from the database are always converted to PHP's ``\DateTimeImmutable``
object or ``null`` if no data is present.

.. note::

    Available since version ``2.6``.

296 297 298 299 300 301 302 303 304
datetimetz
^^^^^^^^^^

Maps and converts date with time and timezone information data.
If you know that the data to be stored always contains date, time and timezone
information, you should consider using this type.
Values retrieved from the database are always converted to PHP's ``\DateTime`` object
or ``null`` if no data is present.

305 306 307 308 309 310 311 312 313 314 315
datetimetz_immutable
^^^^^^^^^^^^^^^^^^^^

The immutable variant of the ``datetimetz`` type.
Values retrieved from the database are always converted to PHP's ``\DateTimeImmutable``
object or ``null`` if no data is present.

.. note::

    Available since version ``2.6``.

316 317 318 319 320 321 322 323 324
time
^^^^

Maps and converts time data without date and timezone information.
If you know that the data to be stored only needs to be a time
without date, time and timezone information, you should consider using this type.
Values retrieved from the database are always converted to PHP's ``\DateTime`` object
or ``null`` if no data is present.

325 326 327 328 329 330 331 332 333 334 335
time_immutable
^^^^^^^^^^^^^^

The immutable variant of the ``time`` type.
Values retrieved from the database are always converted to PHP's ``\DateTimeImmutable``
object or ``null`` if no data is present.

.. note::

    Available since version ``2.6``.

336 337 338 339 340 341 342 343 344
dateinterval
^^^^^^^^^^^^

Maps and converts date and time difference data without timezone information.
If you know that the data to be stored is the difference between two date and time values,
you should consider using this type.
Values retrieved from the database are always converted to PHP's ``\DateInterval`` object
or ``null`` if no data is present.

Benjamin Eberlei's avatar
Benjamin Eberlei committed
345
.. note::
346

347
    See the Known Vendor Issue :doc:`known-vendor-issues` section
348 349 350
    for details about the different handling of microseconds and
    timezones across all the different vendors.

351 352
.. warning::

353
    All date types assume that you are exclusively using the default timezone
354 355 356 357 358
    set by `date_default_timezone_set() <http://docs.php.net/manual/en/function.date-default-timezone-set.php>`_
    or by the php.ini configuration ``date.timezone``.

    If you need specific timezone handling you have to handle this
    in your domain, converting all the values back and forth from UTC.
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
Array types
~~~~~~~~~~~

Types that map array data in different variations such as simple arrays,
real arrays or JSON format arrays.

array
^^^^^

Maps and converts array data based on PHP serialization.
If you need to store an exact representation of your array data,
you should consider using this type as it uses serialization
to represent an exact copy of your array as string in the database.
Values retrieved from the database are always converted to PHP's ``array`` type
using deserialization or ``null`` if no data is present.

.. note::

    This type will always be mapped to the database vendor's ``text`` type
    internally as there is no way of storing a PHP array representation
    natively in the database.
381
    Furthermore this type requires an SQL column comment hint so that it can be
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    reverse engineered from the database. Doctrine cannot map back this type
    properly on vendors not supporting column comments and will fall back to
    ``text`` type instead.

simple_array
^^^^^^^^^^^^

Maps and converts array data based on PHP comma delimited imploding and exploding.
If you know that the data to be stored always is a scalar value based one-dimensional
array, you should consider using this type as it uses simple PHP imploding and
exploding techniques to serialize and deserialize your data.
Values retrieved from the database are always converted to PHP's ``array`` type
using comma delimited ``explode()`` or ``null`` if no data is present.

.. note::

    This type will always be mapped to the database vendor's ``text`` type
    internally as there is no way of storing a PHP array representation
    natively in the database.
401
    Furthermore this type requires an SQL column comment hint so that it can be
402 403 404 405 406 407 408 409 410 411 412
    reverse engineered from the database. Doctrine cannot map back this type
    properly on vendors not supporting column comments and will fall back to
    ``text`` type instead.

.. warning::

    You should never rely on a specific PHP type like ``boolean``,
    ``integer``, ``float`` or ``null`` when retrieving values from
    the database as the ``explode()`` deserialization technique used
    by this type converts every single array item to ``string``.
    This basically means that every array item other than ``string``
Phil Davis's avatar
Phil Davis committed
413
    will lose its type awareness.
414

415 416 417 418 419 420
json
^^^^

Maps and converts array data based on PHP's JSON encoding functions.
If you know that the data to be stored always is in a valid UTF-8
encoded JSON format string, you should consider using this type.
421
Values retrieved from the database are always converted to PHP's ``array`` or
422 423 424 425 426 427 428
``null`` types using PHP's ``json_decode()`` function.

.. note::

    Some vendors have a native JSON type and Doctrine will use it if possible
    and otherwise silently fall back to the vendor's ``text`` type to ensure
    the most efficient storage requirements.
429
    If the vendor does not have a native JSON type, this type requires an SQL
430 431 432 433
    column comment hint so that it can be reverse engineered from the database.
    Doctrine cannot map back this type properly on vendors not supporting column
    comments and will fall back to ``text`` type instead.

434 435 436 437 438 439
.. warning::

    You should never rely on the order of your JSON object keys, as some vendors
    like MySQL sort the keys of its native JSON type using an internal order
    which is also subject to change.

440 441 442
json_array
^^^^^^^^^^

443 444 445 446
.. warning::

    This type is deprecated since 2.6, you should use ``json`` instead.

447 448 449 450 451 452 453 454 455 456 457
Maps and converts array data based on PHP's JSON encoding functions.
If you know that the data to be stored always is in a valid UTF-8
encoded JSON format string, you should consider using this type.
Values retrieved from the database are always converted to PHP's ``array`` type
using PHP's ``json_decode()`` function.

.. note::

    Some vendors have a native JSON type and Doctrine will use it if possible
    and otherwise silently fall back to the vendor's ``text`` type to ensure
    the most efficient storage requirements.
458
    If the vendor does not have a native JSON type, this type requires an SQL
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    column comment hint so that it can be reverse engineered from the database.
    Doctrine cannot map back this type properly on vendors not supporting column
    comments and will fall back to ``text`` type instead.

Object types
~~~~~~~~~~~~

Types that map to objects such as POPOs.

object
^^^^^^

Maps and converts object data based on PHP serialization.
If you need to store an exact representation of your object data,
you should consider using this type as it uses serialization
to represent an exact copy of your object as string in the database.
Values retrieved from the database are always converted to PHP's ``object`` type
using deserialization or ``null`` if no data is present.

.. note::

    This type will always be mapped to the database vendor's ``text`` type
    internally as there is no way of storing a PHP object representation
    natively in the database.
483
    Furthermore this type requires an SQL column comment hint so that it can be
484 485 486 487
    reverse engineered from the database. Doctrine cannot map back this type
    properly on vendors not supporting column comments and will fall back to
    ``text`` type instead.

488 489 490 491 492 493 494 495
.. warning::

    While the built-in ``text`` type of MySQL and MariaDB can store binary data,
    ``mysqldump`` cannot properly export ``text`` fields containing binary data.
    This will cause creating and restoring of backups fail silently. A workaround is
    to ``serialize()``/``unserialize()`` and ``base64_encode()``/``base64_decode()``
    PHP objects and store them into a ``text`` field manually.

496 497
.. warning::

Benjamin Morel's avatar
Benjamin Morel committed
498
    Because the built-in ``text`` type of PostgreSQL does not support NULL bytes,
499 500 501 502
    the object type will cause deserialization errors on PostgreSQL. A workaround is
    to ``serialize()``/``unserialize()`` and ``base64_encode()``/``base64_decode()`` PHP objects and store
    them into a ``text`` field manually.

503 504
.. _mappingMatrix:

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
Mapping Matrix
--------------

The following table shows an overview of Doctrine's type abstraction.
The matrix contains the mapping information for how a specific Doctrine
type is mapped to the database and back to PHP.
Please also notice the mapping specific footnotes for additional information.

+-------------------+---------------+-----------------------------------------------------------------------------------------------+
| Doctrine          | PHP           | Database vendor                                                                               |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | Name                     | Version | Type                                                     |
+===================+===============+==========================+=========+==========================================================+
| **smallint**      | ``integer``   | **MySQL**                | *all*   | ``SMALLINT`` ``UNSIGNED`` [10]_ ``AUTO_INCREMENT`` [11]_ |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Drizzle**              | *all*   | ``INT`` ``UNSIGNED`` [10]_ ``AUTO_INCREMENT`` [11]_      |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``SMALLINT``                                             |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``NUMBER(5)``                                            |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``SMALLINT`` ``IDENTITY`` [11]_                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | *all*   | ``UNSIGNED`` [10]_ ``SMALLINT`` ``IDENTITY`` [11]_       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQLite**               | *all*   | ``INTEGER`` [16]_                                        |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **integer**       | ``integer``   | **MySQL**                | *all*   | ``INT`` ``UNSIGNED`` [10]_ ``AUTO_INCREMENT`` [11]_      |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``INT`` [12]_                                            |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``SERIAL`` [11]_                                         |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``NUMBER(10)``                                           |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``INT`` ``IDENTITY`` [11]_                               |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | *all*   | ``UNSIGNED`` [10]_ ``INT`` ``IDENTITY`` [11]_            |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQLite**               | *all*   | ``INTEGER`` [16]_                                        |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **bigint**        | ``string``    | **MySQL**                | *all*   | ``BIGINT`` ``UNSIGNED`` [10]_ ``AUTO_INCREMENT`` [11]_   |
|                   | [8]_          +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``BIGINT`` [12]_                                         |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``BIGSERIAL`` [11]_                                      |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``NUMBER(20)``                                           |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``BIGINT`` ``IDENTITY`` [11]_                            |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | *all*   | ``UNSIGNED`` [10]_ ``BIGINT`` ``IDENTITY`` [11]_         |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQLite**               | *all*   | ``INTEGER`` [16]_                                        |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
564 565 566
| **decimal** [7]_  | ``string``    | **MySQL**                | *all*   | ``NUMERIC(p, s)`` ``UNSIGNED`` [10]_                     |
|                   | [9]_          +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``NUMERIC(p, s)``                                        |
567 568 569 570 571 572 573 574 575 576 577
|                   |               +--------------------------+         |                                                          |
|                   |               | **Oracle**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Server**           |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
578 579 580
| **float**         | ``float``     | **MySQL**                | *all*   | ``DOUBLE PRECISION`` ``UNSIGNED`` [10]_                  |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``DOUBLE PRECISION``                                     |
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|                   |               +--------------------------+         |                                                          |
|                   |               | **Oracle**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Server**           |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **string**        | ``string``    | **MySQL**                | *all*   | ``VARCHAR(n)`` [3]_                                      |
| [2]_ [5]_         |               +--------------------------+         |                                                          |
|                   |               | **PostgreSQL**           |         |                                                          |
|                   |               +--------------------------+         +----------------------------------------------------------+
|                   |               | **SQL Anywhere**         |         | ``CHAR(n)`` [4]_                                         |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Drizzle**              | *all*   | ``VARCHAR(n)``                                           |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``VARCHAR2(n)`` [3]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``CHAR(n)`` [4]_                                         |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``NVARCHAR(n)`` [3]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``NCHAR(n)`` [4]_                                        |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **text**          | ``string``    | **MySQL**                | *all*   | ``TINYTEXT`` [17]_                                       |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``TEXT`` [18]_                                           |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``MEDIUMTEXT`` [19]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``LONGTEXT`` [20]_                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TEXT``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``CLOB``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``VARCHAR(MAX)``                                         |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
630
| **guid**          | ``string``    | **MySQL**                | *all*   | ``VARCHAR(255)`` [1]_                                    |
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
|                   |               +--------------------------+         |                                                          |
|                   |               | **Oracle**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``UNIQUEIDENTIFIER``                                     |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``UUID``                                                 |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **binary**        | ``resource``  | **MySQL**                | *all*   | ``VARBINARY(n)`` [3]_                                    |
| [2]_ [6]_         |               +--------------------------+         |                                                          |
|                   |               | **SQL Server**           |         +----------------------------------------------------------+
|                   |               +--------------------------+         | ``BINARY(n)`` [4]_                                       |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Drizzle**              | *all*   | ``VARBINARY(n)``                                         |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``RAW(n)``                                               |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``BYTEA`` [16]_                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQLite**               | *all*   | ``BLOB`` [16]_                                           |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **blob**          | ``resource``  | **MySQL**                | *all*   | ``TINYBLOB`` [17]_                                       |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``BLOB`` [18]_                                           |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``MEDIUMBLOB`` [19]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``LONGBLOB`` [20]_                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``BLOB``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``VARBINARY(MAX)``                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | *all*   | ``LONG BINARY``                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``BYTEA``                                                |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **boolean**       | ``boolean``   | **MySQL**                | *all*   | ``TINYINT(1)``                                           |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``BOOLEAN``                                              |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``BIT``                                                  |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``NUMBER(1)``                                            |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **date**          | ``\DateTime`` | **MySQL**                | *all*   | ``DATE``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **PostgreSQL**           |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Oracle**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+                                                          |
|                   |               | **SQL Server**           | >= 2008 |                                                          |
|                   |               |                          +---------+----------------------------------------------------------+
|                   |               |                          | < 2008  | ``DATETIME`` [16]_                                       |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **datetime**      | ``\DateTime`` | **MySQL**                | *all*   | ``DATETIME`` [13]_                                       |
|                   |               +--------------------------+         +----------------------------------------------------------+
|                   |               | **Drizzle**              |         | ``TIMESTAMP`` [14]_                                      |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``DATETIME``                                             |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TIMESTAMP(0) WITHOUT TIME ZONE``                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``TIMESTAMP(0)``                                         |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **datetimetz**    | ``\DateTime`` | **MySQL**                | *all*   | ``DATETIME``  [15]_ [16]_                                |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+                                                          |
|                   |               | **SQL Server**           | < 2008  |                                                          |
|                   |               |                          +---------+----------------------------------------------------------+
|                   |               |                          | >= 2008 | ``DATETIMEOFFSET(6)``                                    |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TIMESTAMP(0) WITH TIME ZONE``                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Oracle**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | < 12    | ``DATETIME``  [15]_ [16]_                                |
|                   |               |                          +---------+----------------------------------------------------------+
|                   |               |                          | >= 12   | ``TIMESTAMP WITH TIME ZONE``                             |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **time**          | ``\DateTime`` | **MySQL**                | *all*   | ``TIME``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TIME(0) WITHOUT TIME ZONE``                            |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``DATE`` [16]_                                           |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | < 2008  | ``DATETIME`` [16]_                                       |
|                   |               |                          +---------+----------------------------------------------------------+
|                   |               |                          | >= 2008 | ``TIME(0)``                                              |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **array** [1]_    | ``array``     | **MySQL**                | *all*   | ``TINYTEXT`` [17]_                                       |
+-------------------+               |                          |         +----------------------------------------------------------+
| **simple array**  |               |                          |         | ``TEXT`` [18]_                                           |
| [1]_              |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``MEDIUMTEXT`` [19]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``LONGTEXT`` [20]_                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TEXT``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``CLOB``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``VARCHAR(MAX)``                                         |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **json_array**    | ``array``     | **MySQL** [1]_           | *all*   | ``TINYTEXT`` [17]_                                       |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``TEXT`` [18]_                                           |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``MEDIUMTEXT`` [19]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``LONGTEXT`` [20]_                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
784
|                   |               | **PostgreSQL**           | < 9.2   | ``TEXT`` [1]_                                            |
785
|                   |               |                          +---------+----------------------------------------------------------+
786 787 788 789 790 791 792
|                   |               |                          | < 9.4   | ``JSON``                                                 |
|                   |               |                          +---------+----------------------------------------------------------+
|                   |               |                          | >= 9.4  | ``JSON`` [21]_                                           |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``JSONB`` [22]_                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Anywhere**         | *all*   | ``TEXT`` [1]_                                            |
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``CLOB`` [1]_                                            |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``VARCHAR(MAX)`` [1]_                                    |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **object** [1]_   | ``object``    | **MySQL**                | *all*   | ``TINYTEXT`` [17]_                                       |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``TEXT`` [18]_                                           |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``MEDIUMTEXT`` [19]_                                     |
|                   |               |                          |         +----------------------------------------------------------+
|                   |               |                          |         | ``LONGTEXT`` [20]_                                       |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **PostgreSQL**           | *all*   | ``TEXT``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQL Anywhere**         |         |                                                          |
|                   |               +--------------------------+         |                                                          |
|                   |               | **Drizzle**              |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **Oracle**               | *all*   | ``CLOB``                                                 |
|                   |               +--------------------------+         |                                                          |
|                   |               | **SQLite**               |         |                                                          |
|                   |               +--------------------------+---------+----------------------------------------------------------+
|                   |               | **SQL Server**           | *all*   | ``VARCHAR(MAX)``                                         |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+

.. [1] Requires hint in the column comment for proper reverse engineering of the appropriate
       Doctrine type mapping.
.. [2] **n** is the **length** attribute set in the column definition (defaults to 255 if omitted).
.. [3] Chosen if the column definition has the **fixed** attribute set to ``false`` (default).
.. [4] Chosen if the column definition has the **fixed** attribute set to ``true``.
.. [5] Silently maps to the vendor specific ``text`` type if the given **length** attribute for
       **n** exceeds the maximum length the related platform allows. If this is the case, please
       see [16]_.
.. [6] Silently maps to the vendor specific ``blob`` type if the given **length** attribute for
       **n** exceeds the maximum length the related platform allows. If this is the case, please
       see [16]_.
.. [7] **p** is the precision and **s** the scale set in the column definition.
       The precision defaults to ``10`` and the scale to ``0`` if not set.
.. [8] Returns PHP ``string`` type value instead of ``integer`` because of maximum integer value
       implications on non 64bit platforms.
.. [9] Returns PHP ``string`` type value instead of ``double`` because of PHP's limitation in
       preserving the exact precision when casting to ``double``.
.. [10] Used if **unsigned** attribute is set to ``true`` in the column definition (default ``false``).
.. [11] Used if **autoincrement** attribute is set to ``true`` in the column definition (default ``false``).
.. [12] Chosen if the column definition has the **autoincrement** attribute set to ``false`` (default).
843
.. [13] Chosen if the column definition does not contain the **version** option inside the **platformOptions**
844 845 846 847 848 849 850 851 852 853 854 855 856 857
        attribute array or is set to ``false`` which marks it as a non-locking information column.
.. [14] Chosen if the column definition contains the **version** option inside the **platformOptions**
        attribute array and is set to ``true`` which marks it as a locking information column.
.. [15] Fallback type as the vendor does not support a native date time type with timezone information.
        This means that the timezone information gets lost when storing a value.
.. [16] Cannot be safely reverse engineered to the same Doctrine type as the vendor does not have a
        native distinct data type for this mapping. Using this type with this vendor can therefore
        have implications on schema comparison (*online* vs *offline* schema) and PHP type safety
        (data conversion from database to PHP value) because it silently falls back to its
        appropriate Doctrine type.
.. [17] Chosen if the column length is less or equal to **2 ^  8 - 1 = 255**.
.. [18] Chosen if the column length is less or equal to **2 ^ 16 - 1 = 65535**.
.. [19] Chosen if the column length is less or equal to **2 ^ 24 - 1 = 16777215**.
.. [20] Chosen if the column length is less or equal to **2 ^ 32 - 1 = 4294967295** or empty.
858 859 860 861
.. [21] Chosen if the column definition does not contain the **jsonb** option inside the **platformOptions**
        attribute array or is set to ``false``.
.. [22] Chosen if the column definition contains the **jsonb** option inside the **platformOptions**
        attribute array and is set to ``true``.
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
Detection of Database Types
---------------------------

When calling table inspection methods on your connections
``SchemaManager`` instance the retrieved database column types are
translated into Doctrine mapping types. Translation is necessary to
allow database abstraction and metadata comparisons for example for
Migrations or the ORM SchemaTool.

Each database platform has a default mapping of database types to
Doctrine types. You can inspect this mapping for platform of your
choice looking at the
``AbstractPlatform::initializeDoctrineTypeMappings()``
implementation.

If you want to change how Doctrine maps a database type to a
``Doctrine\DBAL\Types\Type`` instance you can use the
``AbstractPlatform::registerDoctrineTypeMapping($dbType, $doctrineType)``
method to add new database types or overwrite existing ones.

Benjamin Eberlei's avatar
Benjamin Eberlei committed
883
.. note::
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

    You can only map a database type to exactly one Doctrine type.
    Database vendors that allow to define custom types like PostgreSql
    can help to overcome this issue.

Custom Mapping Types
--------------------

Just redefining how database types are mapped to all the existing
Doctrine types is not at all that useful. You can define your own
Doctrine Mapping Types by extending ``Doctrine\DBAL\Types\Type``.
You are required to implement 4 different methods to get this
working.

See this example of how to implement a Money object in PostgreSQL.
For this we create the type in PostgreSQL as:

.. code-block:: sql

    CREATE DOMAIN MyMoney AS DECIMAL(18,3);

Now we implement our ``Doctrine\DBAL\Types\Type`` instance:

::

    <?php
    namespace My\Project\Types;
911

912 913
    use Doctrine\DBAL\Types\Type;
    use Doctrine\DBAL\Platforms\AbstractPlatform;
914

915 916 917 918 919 920
    /**
     * My custom datatype.
     */
    class MoneyType extends Type
    {
        const MONEY = 'money'; // modify to match your type name
921

922
        public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
923 924 925
        {
            return 'MyMoney';
        }
926

927 928 929 930
        public function convertToPHPValue($value, AbstractPlatform $platform)
        {
            return new Money($value);
        }
931

932 933 934 935
        public function convertToDatabaseValue($value, AbstractPlatform $platform)
        {
            return $value->toDecimal();
        }
936

937 938 939 940 941 942
        public function getName()
        {
            return self::MONEY;
        }
    }

943 944 945
The job of Doctrine-DBAL is to transform your type into an SQL
declaration. You can modify the SQL declaration Doctrine will produce.
At first, to enable this feature, you must override the
Grégoire Paris's avatar
Grégoire Paris committed
946
``canRequireSQLConversion`` method:
947 948 949 950 951 952 953 954 955

::

    <?php
    public function canRequireSQLConversion()
    {
        return true;
    }

Grégoire Paris's avatar
Grégoire Paris committed
956 957
Then you override the ``convertToPhpValueSQL`` and
``convertToDatabaseValueSQL`` methods :
958 959 960 961 962 963

::

    <?php
    public function convertToPHPValueSQL($sqlExpr, $platform)
    {
Julien Fastré's avatar
Julien Fastré committed
964
        return 'MyMoneyFunction(\''.$sqlExpr.'\') ';
965
    }
966

967 968 969 970 971
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
    {
        return 'MyFunction('.$sqlExpr.')';
    }

972 973 974 975 976 977 978 979 980
Now we have to register this type with the Doctrine Type system and
hook it into the database platform:

::

    <?php
    Type::addType('money', 'My\Project\Types\MoneyType');
    $conn->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'money');

Phil Davis's avatar
Phil Davis committed
981
This would allow using a money type in the ORM for example and
982 983
have Doctrine automatically convert it back and forth to the
database.