Skip to content

Commit

Permalink
Merge pull request doctrine#4414 from morozov/issues/4406
Browse files Browse the repository at this point in the history
ResultCacheStatement::fetchAllAssociative does not store results in cache
  • Loading branch information
greg0ire authored Nov 8, 2020
2 parents 3622d6d + 12508e6 commit 97ee7c4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
16 changes: 6 additions & 10 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public function fetchAllNumeric(): array
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

$this->store($data);
$this->data = $data;

$this->saveToCache();

return array_map('array_values', $data);
}
Expand All @@ -258,7 +260,9 @@ public function fetchAllAssociative(): array
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

$this->store($data);
$this->data = $data;

$this->saveToCache();

return $data;
}
Expand Down Expand Up @@ -322,14 +326,6 @@ private function doFetch()
return false;
}

/**
* @param array<int,array<string,mixed>> $data
*/
private function store(array $data): void
{
$this->data = $data;
}

private function saveToCache(): void
{
if ($this->data === null) {
Expand Down
39 changes: 37 additions & 2 deletions tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Logging\DebugStack;
Expand Down Expand Up @@ -212,7 +213,10 @@ public function testDontFinishNoCache(): void
self::assertCount(2, $this->sqlLogger->queries);
}

public function testFetchAllSavesCache(): void
/**
* @dataProvider fetchAllProvider
*/
public function testFetchingAllRowsSavesCache(callable $fetchAll): void
{
$layerCache = new ArrayCache();

Expand All @@ -222,11 +226,42 @@ public function testFetchAllSavesCache(): void
[],
new QueryCacheProfile(0, 'testcachekey', $layerCache)
);
$stmt->fetchAll();

$fetchAll($stmt);

self::assertCount(1, $layerCache->fetch('testcachekey'));
}

/**
* @return iterable<string,list<mixed>>
*/
public static function fetchAllProvider(): iterable
{
yield 'fetchAll' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAll();
},
];

yield 'fetchAllAssociative' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAllAssociative();
},
];

yield 'fetchAllNumeric' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAllNumeric();
},
];

yield 'fetchFirstColumn' => [
static function (ResultCacheStatement $result): void {
$result->fetchFirstColumn();
},
];
}

public function testFetchAllColumn(): void
{
$query = $this->connection->getDatabasePlatform()
Expand Down

0 comments on commit 97ee7c4

Please sign in to comment.