Skip to content

Commit b9a05eb

Browse files
committed
DbalUtils: Add integer type (MySQL 8 breaks on non integer for TIMESTAMP column)
1 parent fe88876 commit b9a05eb

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

src/InnerResultIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function executeQuery(): void
9797

9898
$this->logger->debug('Running SQL request: '.$sql);
9999

100-
$this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateArrayTypes($this->parameters));
100+
$this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateTypes($this->parameters));
101101

102102
$this->fetchStarted = true;
103103
}

src/ResultIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function executeCountQuery(): void
8989
{
9090
$sql = $this->magicQuery->buildPreparedStatement($this->queryFactory->getMagicSqlCount(), $this->parameters);
9191
$this->logger->debug('Running count query: '.$sql);
92-
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters, 0, DbalUtils::generateArrayTypes($this->parameters));
92+
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters, 0, DbalUtils::generateTypes($this->parameters));
9393
}
9494

9595
/**

src/Utils/DbalUtils.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\TDBM\Utils;
55

66
use Doctrine\DBAL\Connection;
7+
use Doctrine\DBAL\ParameterType;
78
use function is_array;
89
use function is_int;
910

@@ -14,11 +15,12 @@ class DbalUtils
1415
{
1516
/**
1617
* If a parameter is an array (used in a "IN" statement), we need to tell Doctrine about it.
18+
* If it is an integer we have to tell DBAL (default is string)
1719
* @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
1820
* @param array<string, mixed> $parameters
1921
* @return array<string, int>
2022
*/
21-
public static function generateArrayTypes(array $parameters): array
23+
public static function generateTypes(array $parameters): array
2224
{
2325
$types = [];
2426
foreach ($parameters as $key => $value) {
@@ -30,6 +32,8 @@ public static function generateArrayTypes(array $parameters): array
3032
}
3133
}
3234
$types[$key] = Connection::PARAM_INT_ARRAY;
35+
} elseif (is_int($value)) {
36+
$types[$key] = ParameterType::INTEGER;
3337
}
3438
}
3539

tests/Utils/DbalUtilsTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
namespace TheCodingMachine\TDBM\Utils;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\ParameterType;
67
use PHPUnit\Framework\TestCase;
78

89
class DbalUtilsTest extends TestCase
910
{
10-
public function testGenerateArrayTypes(): void
11+
public function testGenerateTypes(): void
1112
{
1213
$params = [
1314
'key1' => 'foo',
1415
'key2' => [1,2,3],
1516
'key3' => [1,2,'baz'],
17+
'key4' => 1,
1618
];
1719

18-
$this->assertSame(['key2'=>Connection::PARAM_INT_ARRAY, 'key3'=>Connection::PARAM_STR_ARRAY], DbalUtils::generateArrayTypes($params));
20+
$this->assertSame(['key2'=>Connection::PARAM_INT_ARRAY, 'key3'=>Connection::PARAM_STR_ARRAY, 'key4'=>ParameterType::INTEGER], DbalUtils::generateTypes($params));
1921
}
2022
}

0 commit comments

Comments
 (0)