Skip to content

Commit

Permalink
Fixed query result caching when FetchMode::COLUMN is used
Browse files Browse the repository at this point in the history
Internally, ArrayStatement expects every row to be represented as an array regardless of the fetch mode, however FetchMode::COLUMN produces one value per row.
  • Loading branch information
morozov committed Oct 8, 2019
1 parent 66f2811 commit 08dca0d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
$this->data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);

if ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $key => $value) {
$data[$key] = [$value];
}
}

$this->data = $data;
$this->emptied = true;

return $this->data;
Expand Down
16 changes: 16 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ public function testFetchAllAndFinishSavesCache()
self::assertCount(1, $layerCache->fetch('testcachekey'));
}

public function testFetchAllColumn() : void
{
$query = $this->connection->getDatabasePlatform()
->getDummySelectSQL('1');

$qcp = new QueryCacheProfile(0, 0, new ArrayCache());

$stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
$stmt->fetchAll(FetchMode::COLUMN);
$stmt->closeCursor();

$stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);

self::assertEquals([1], $stmt->fetchAll(FetchMode::COLUMN));
}

public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchMode)
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
Expand Down

0 comments on commit 08dca0d

Please sign in to comment.