Skip to content

Commit

Permalink
Merge branch '2.13.x' into 3.1.x
Browse files Browse the repository at this point in the history
* 2.13.x:
  add a note about types
  docs: fix docs for the json type
  Fix integer decimal casting to string in PHP 8.1 with SQLite
  • Loading branch information
derrabus committed Nov 25, 2021
2 parents 0673a9e + d0bae1b commit 1879160
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
22 changes: 13 additions & 9 deletions docs/en/reference/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,18 @@ 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.
Values retrieved from the database are always converted to PHP's ``array`` or
``null`` types using PHP's ``json_decode()`` function.
Values retrieved from the database are always converted to PHP's
native types using PHP's ``json_decode()`` function.
JSON objects are always converted to PHP associative arrays.

.. note::

The ``json`` type doesn't preserve the type of PHP objects.
PHP objects will always be encoded as (anonymous) JSON objects.
JSON objects will always be decoded as PHP associative arrays.

To preserve the type of PHP objects, consider using
`Doctrine JSON ODM <https://github.com/dunglas/doctrine-json-odm>`_.

.. note::

Expand Down Expand Up @@ -711,13 +721,7 @@ Please also notice the mapping specific footnotes for additional information.
| | +--------------------------+---------+----------------------------------------------------------+
| | | **SQL Server** | *all* | ``VARCHAR(MAX)`` |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **json_array** | ``array`` | **MySQL** [1]_ | *all* | ``TINYTEXT`` [16]_ |
| | | | +----------------------------------------------------------+
| | | | | ``TEXT`` [17]_ |
| | | | +----------------------------------------------------------+
| | | | | ``MEDIUMTEXT`` [18]_ |
| | | | +----------------------------------------------------------+
| | | | | ``LONGTEXT`` [19]_ |
| **json** | ``mixed`` | **MySQL** | *all* | ``JSON`` |
| | +--------------------------+---------+----------------------------------------------------------+
| | | **PostgreSQL** | *all* | ``JSON`` [20]_ |
| | | | +----------------------------------------------------------+
Expand Down
5 changes: 3 additions & 2 deletions src/Types/DecimalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;

use function is_float;
use function is_int;

use const PHP_VERSION_ID;

Expand Down Expand Up @@ -34,9 +35,9 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
// Some drivers starting from PHP 8.1 can represent decimals as float
// Some drivers starting from PHP 8.1 can represent decimals as float/int
// See also: https://github.com/doctrine/dbal/pull/4818
if (PHP_VERSION_ID >= 80100 && is_float($value)) {
if (PHP_VERSION_ID >= 80100 && (is_float($value) || is_int($value))) {
return (string) $value;
}

Expand Down
28 changes: 25 additions & 3 deletions tests/Functional/Types/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

use function rtrim;

final class DecimalTest extends FunctionalTestCase
{
public function testInsertAndRetrieveDecimal(): void
/**
* @return string[][]
*/
public function dataValuesProvider(): array
{
return [
['13.37'],
['13.0'],
];
}

/**
* @dataProvider dataValuesProvider
*/
public function testInsertAndRetrieveDecimal(string $expected): void
{
$table = new Table('decimal_table');
$table->addColumn('val', Types::DECIMAL, ['precision' => 4, 'scale' => 2]);
Expand All @@ -21,7 +37,7 @@ public function testInsertAndRetrieveDecimal(): void

$this->connection->insert(
'decimal_table',
['val' => '13.37'],
['val' => $expected],
['val' => Types::DECIMAL]
);

Expand All @@ -30,6 +46,12 @@ public function testInsertAndRetrieveDecimal(): void
$this->connection->getDatabasePlatform()
);

self::assertSame('13.37', $value);
self::assertIsString($value);
self::assertSame($this->stripTrailingZero($expected), $this->stripTrailingZero($value));
}

private function stripTrailingZero(string $expected): string
{
return rtrim(rtrim($expected, '0'), '.');
}
}

0 comments on commit 1879160

Please sign in to comment.