Skip to content

Commit c08d3bc

Browse files
authored
Merge pull request #4034 from morozov/deprecate-fetch-mode
Additional changes based on the discussion in #4007
2 parents ce21834 + e74b97c commit c08d3bc

File tree

4 files changed

+66
-59
lines changed

4 files changed

+66
-59
lines changed

docs/en/reference/caching.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object is closed:
4242

4343
<?php
4444
$stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
45-
$data = $stmt->fetchAll();
45+
$data = $stmt->fetchAllAssociative();
4646
$stmt->closeCursor(); // at this point the result is cached
4747

4848
.. warning::

docs/en/reference/data-retrieval-and-manipulation.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ the query until there are no more rows:
4141
4242
<?php
4343
44-
while ($row = $stmt->fetch()) {
44+
while (($row = $stmt->fetchAssociative()) !== false) {
4545
echo $row['headline'];
4646
}
4747
@@ -308,7 +308,7 @@ Prepare a given SQL statement and return the
308308
<?php
309309
$statement = $conn->prepare('SELECT * FROM user');
310310
$statement->execute();
311-
$users = $statement->fetchAll();
311+
$users = $statement->fetchAllAssociative();
312312
313313
/*
314314
array(
@@ -346,7 +346,7 @@ parameters to the execute method, then returning the statement:
346346
347347
<?php
348348
$statement = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
349-
$user = $statement->fetch();
349+
$user = $statement->fetchAssociative();
350350
351351
/*
352352
array(
@@ -360,15 +360,15 @@ to perform necessary type conversions between actual input
360360
parameters and expected database values. See the
361361
:ref:`Types <mappingMatrix>` section for more information.
362362

363-
fetchAll()
364-
~~~~~~~~~~
363+
fetchAllAssociative()
364+
~~~~~~~~~~~~~~~~~~~~~
365365

366366
Execute the query and fetch all results into an array:
367367

368368
.. code-block:: php
369369
370370
<?php
371-
$users = $conn->fetchAll('SELECT * FROM user');
371+
$users = $conn->fetchAllAssociative('SELECT * FROM user');
372372
373373
/*
374374
array(
@@ -379,15 +379,15 @@ Execute the query and fetch all results into an array:
379379
)
380380
*/
381381
382-
fetchArray()
383-
~~~~~~~~~~~~
382+
fetchNumeric()
383+
~~~~~~~~~~~~~~
384384

385385
Numeric index retrieval of first result row of the given query:
386386

387387
.. code-block:: php
388388
389389
<?php
390-
$user = $conn->fetchArray('SELECT * FROM user WHERE username = ?', array('jwage'));
390+
$user = $conn->fetchNumeric('SELECT * FROM user WHERE username = ?', array('jwage'));
391391
392392
/*
393393
array(
@@ -396,26 +396,26 @@ Numeric index retrieval of first result row of the given query:
396396
)
397397
*/
398398
399-
fetchColumn()
400-
~~~~~~~~~~~~~
399+
fetchOne()
400+
~~~~~~~~~~
401401

402-
Retrieve only the given column of the first result row.
402+
Retrieve only the value of the first column of the first result row.
403403

404404
.. code-block:: php
405405
406406
<?php
407-
$username = $conn->fetchColumn('SELECT username FROM user WHERE id = ?', array(1), 0);
407+
$username = $conn->fetchOne('SELECT username FROM user WHERE id = ?', array(1), 0);
408408
echo $username; // jwage
409409
410-
fetchAssoc()
411-
~~~~~~~~~~~~
410+
fetchAssociative()
411+
~~~~~~~~~~~~~~~~~~
412412

413-
Retrieve assoc row of the first result row.
413+
Retrieve associative array of the first result row.
414414

415415
.. code-block:: php
416416
417417
<?php
418-
$user = $conn->fetchAssoc('SELECT * FROM user WHERE username = ?', array('jwage'));
418+
$user = $conn->fetchAssociative('SELECT * FROM user WHERE username = ?', array('jwage'));
419419
/*
420420
array(
421421
'username' => 'jwage',

lib/Doctrine/DBAL/Cache/ResultCacheStatement.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use InvalidArgumentException;
1414
use IteratorAggregate;
1515
use PDO;
16+
use function array_map;
1617
use function array_merge;
1718
use function array_values;
1819
use function assert;
@@ -55,7 +56,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
5556
*/
5657
private $emptied = false;
5758

58-
/** @var mixed[] */
59+
/** @var array<int,array<string,mixed>> */
5960
private $data;
6061

6162
/** @var int */
@@ -179,18 +180,26 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
179180
*/
180181
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
181182
{
182-
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
183-
184-
if ($fetchMode === FetchMode::COLUMN) {
185-
foreach ($data as $key => $value) {
186-
$data[$key] = [$value];
187-
}
188-
}
183+
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE, $fetchArgument, $ctorArgs);
189184

190185
$this->data = $data;
191186
$this->emptied = true;
192187

193-
return $this->data;
188+
if ($fetchMode === FetchMode::NUMERIC) {
189+
foreach ($data as $i => $row) {
190+
$data[$i] = array_values($row);
191+
}
192+
} elseif ($fetchMode === FetchMode::MIXED) {
193+
foreach ($data as $i => $row) {
194+
$data[$i] = array_merge($row, array_values($row));
195+
}
196+
} elseif ($fetchMode === FetchMode::COLUMN) {
197+
foreach ($data as $i => $row) {
198+
$data[$i] = reset($row);
199+
}
200+
}
201+
202+
return $data;
194203
}
195204

196205
/**
@@ -247,7 +256,9 @@ public function fetchAllNumeric() : array
247256
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
248257
}
249258

250-
return $this->store($data);
259+
$this->store($data);
260+
261+
return array_map('array_values', $this->data);
251262
}
252263

253264
/**
@@ -261,7 +272,9 @@ public function fetchAllAssociative() : array
261272
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
262273
}
263274

264-
return $this->store($data);
275+
$this->store($data);
276+
277+
return $this->data;
265278
}
266279

267280
/**
@@ -311,19 +324,11 @@ private function doFetch()
311324
}
312325

313326
/**
314-
* @param array<int,array<mixed>> $data
315-
*
316-
* @return array<int,array<mixed>>
327+
* @param array<int,array<string,mixed>> $data
317328
*/
318-
private function store(array $data) : array
329+
private function store(array $data) : void
319330
{
320-
foreach ($data as $key => $value) {
321-
$data[$key] = [$value];
322-
}
323-
324331
$this->data = $data;
325332
$this->emptied = true;
326-
327-
return $this->data;
328333
}
329334
}

tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ public function testMixingFetch() : void
102102
$numExpectedResult[] = array_values($v);
103103
}
104104

105-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
105+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
106106

107107
$data = $this->hydrateStmt($stmt, FetchMode::ASSOCIATIVE);
108108

109109
self::assertEquals($this->expectedResult, $data);
110110

111-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
111+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
112112

113113
$data = $this->hydrateStmt($stmt, FetchMode::NUMERIC);
114114

@@ -124,26 +124,26 @@ public function testIteratorFetch() : void
124124

125125
private function assertStandardAndIteratorFetchAreEqual(int $fetchMode) : void
126126
{
127-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
127+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
128128
$data = $this->hydrateStmt($stmt, $fetchMode);
129129

130-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
130+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
131131
$dataIterator = $this->hydrateStmtIterator($stmt, $fetchMode);
132132

133133
self::assertEquals($data, $dataIterator);
134134
}
135135

136136
public function testDontCloseNoCache() : void
137137
{
138-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
138+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
139139

140140
$data = [];
141141

142142
while ($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) {
143143
$data[] = $row;
144144
}
145145

146-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
146+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
147147

148148
$data = [];
149149

@@ -156,12 +156,12 @@ public function testDontCloseNoCache() : void
156156

157157
public function testDontFinishNoCache() : void
158158
{
159-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
159+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
160160

161161
$stmt->fetch(FetchMode::ASSOCIATIVE);
162162
$stmt->closeCursor();
163163

164-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
164+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
165165

166166
$this->hydrateStmt($stmt, FetchMode::NUMERIC);
167167

@@ -171,7 +171,7 @@ public function testDontFinishNoCache() : void
171171
public function testFetchAllAndFinishSavesCache() : void
172172
{
173173
$layerCache = new ArrayCache();
174-
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'testcachekey', $layerCache));
174+
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'testcachekey', $layerCache));
175175
$stmt->fetchAll();
176176
$stmt->closeCursor();
177177

@@ -199,13 +199,13 @@ public function testFetchAllColumn() : void
199199
*/
200200
private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, int $fetchMode) : void
201201
{
202-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
202+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
203203

204204
self::assertEquals(2, $stmt->columnCount());
205205
$data = $this->hydrateStmt($stmt, $fetchMode);
206206
self::assertEquals($expectedResult, $data);
207207

208-
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
208+
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
209209

210210
self::assertEquals(2, $stmt->columnCount());
211211
$data = $this->hydrateStmt($stmt, $fetchMode);
@@ -215,23 +215,24 @@ private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedR
215215

216216
public function testEmptyResultCache() : void
217217
{
218-
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
219-
$data = $this->hydrateStmt($stmt);
218+
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
219+
$this->hydrateStmt($stmt);
220220

221-
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
222-
$data = $this->hydrateStmt($stmt);
221+
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
222+
$this->hydrateStmt($stmt);
223223

224224
self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
225225
}
226226

227227
public function testChangeCacheImpl() : void
228228
{
229-
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
230-
$data = $this->hydrateStmt($stmt);
229+
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
230+
$this->hydrateStmt($stmt);
231231

232232
$secondCache = new ArrayCache();
233-
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey', $secondCache));
234-
$data = $this->hydrateStmt($stmt);
233+
234+
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey', $secondCache));
235+
$this->hydrateStmt($stmt);
235236

236237
self::assertCount(2, $this->sqlLogger->queries, 'two hits');
237238
self::assertCount(1, $secondCache->fetch('emptycachekey'));
@@ -243,7 +244,8 @@ public function testChangeCacheImpl() : void
243244
private function hydrateStmt(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
244245
{
245246
$data = [];
246-
while ($row = $stmt->fetch($fetchMode)) {
247+
248+
foreach ($stmt->fetchAll($fetchMode) as $row) {
247249
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
248250
}
249251

0 commit comments

Comments
 (0)