Loading core/lib/Drupal/Core/Database/StatementWrapperIterator.php +4 −4 Original line number Diff line number Diff line Loading @@ -157,10 +157,10 @@ public function fetchAllAssoc($key, $fetch = NULL) { return []; } // Once the foreach loop is completed, the resultset is marked so not to // Once the while loop is completed, the resultset is marked so not to // allow more fetching. $return = []; foreach ($this as $record) { while ($record = $this->fetch()) { $recordKey = is_object($record) ? $record->$key : $record[$key]; $return[$recordKey] = $record; } Loading @@ -179,10 +179,10 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1) { return []; } // Once the foreach loop is completed, the resultset is marked so not to // Once the while loop is completed, the resultset is marked so not to // allow more fetching. $return = []; foreach ($this as $record) { while ($record = $this->fetch()) { $return[$record[$key_index]] = $record[$value_index]; } return $return; Loading core/tests/Drupal/KernelTests/Core/Database/StatementTest.php +93 −0 Original line number Diff line number Diff line Loading @@ -64,6 +64,99 @@ public function testIteratedStatementFetch(): void { $this->assertFalse($statement->fetchField()); } /** * Tests statement fetchAll after a partial traversal. */ public function testPartiallyIteratedStatementFetchAll(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ 0 => (object) [ "id" => "3", "name" => "Ringo", "age" => "28", "job" => "Drummer", ], 1 => (object) [ "id" => "4", "name" => "Paul", "age" => "26", "job" => "Songwriter", ], ]; $this->assertEquals($expected, $statement->fetchAll()); $this->assertSame([], $statement->fetchAll()); } /** * Tests statement fetchAllKeyed after a partial traversal. */ public function testPartiallyIteratedStatementFetchAllKeyed(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ "3" => "Ringo", "4" => "Paul", ]; $this->assertSame($expected, $statement->fetchAllKeyed()); $this->assertSame([], $statement->fetchAllKeyed()); } /** * Tests statement fetchAllAssoc after a partial traversal. */ public function testPartiallyIteratedStatementFetchAllAssoc(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ "28" => (object) [ "id" => "3", "name" => "Ringo", "age" => "28", "job" => "Drummer", ], "26" => (object) [ "id" => "4", "name" => "Paul", "age" => "26", "job" => "Songwriter", ], ]; $this->assertEquals($expected, $statement->fetchAllAssoc('age')); $this->assertSame([], $statement->fetchAllAssoc('age')); } /** * Tests statement fetchCol after a partial traversal. */ public function testPartiallyIteratedStatementFetchCol(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = ["3", "4"]; $this->assertSame($expected, $statement->fetchCol()); $this->assertSame([], $statement->fetchCol()); } /** * Tests statement rewinding. */ Loading Loading
core/lib/Drupal/Core/Database/StatementWrapperIterator.php +4 −4 Original line number Diff line number Diff line Loading @@ -157,10 +157,10 @@ public function fetchAllAssoc($key, $fetch = NULL) { return []; } // Once the foreach loop is completed, the resultset is marked so not to // Once the while loop is completed, the resultset is marked so not to // allow more fetching. $return = []; foreach ($this as $record) { while ($record = $this->fetch()) { $recordKey = is_object($record) ? $record->$key : $record[$key]; $return[$recordKey] = $record; } Loading @@ -179,10 +179,10 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1) { return []; } // Once the foreach loop is completed, the resultset is marked so not to // Once the while loop is completed, the resultset is marked so not to // allow more fetching. $return = []; foreach ($this as $record) { while ($record = $this->fetch()) { $return[$record[$key_index]] = $record[$value_index]; } return $return; Loading
core/tests/Drupal/KernelTests/Core/Database/StatementTest.php +93 −0 Original line number Diff line number Diff line Loading @@ -64,6 +64,99 @@ public function testIteratedStatementFetch(): void { $this->assertFalse($statement->fetchField()); } /** * Tests statement fetchAll after a partial traversal. */ public function testPartiallyIteratedStatementFetchAll(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ 0 => (object) [ "id" => "3", "name" => "Ringo", "age" => "28", "job" => "Drummer", ], 1 => (object) [ "id" => "4", "name" => "Paul", "age" => "26", "job" => "Songwriter", ], ]; $this->assertEquals($expected, $statement->fetchAll()); $this->assertSame([], $statement->fetchAll()); } /** * Tests statement fetchAllKeyed after a partial traversal. */ public function testPartiallyIteratedStatementFetchAllKeyed(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ "3" => "Ringo", "4" => "Paul", ]; $this->assertSame($expected, $statement->fetchAllKeyed()); $this->assertSame([], $statement->fetchAllKeyed()); } /** * Tests statement fetchAllAssoc after a partial traversal. */ public function testPartiallyIteratedStatementFetchAllAssoc(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = [ "28" => (object) [ "id" => "3", "name" => "Ringo", "age" => "28", "job" => "Drummer", ], "26" => (object) [ "id" => "4", "name" => "Paul", "age" => "26", "job" => "Songwriter", ], ]; $this->assertEquals($expected, $statement->fetchAllAssoc('age')); $this->assertSame([], $statement->fetchAllAssoc('age')); } /** * Tests statement fetchCol after a partial traversal. */ public function testPartiallyIteratedStatementFetchCol(): void { $statement = $this->connection->query('SELECT * FROM {test}'); for ($i = 0; $i < 2; $i++) { $statement->fetch(); } $expected = ["3", "4"]; $this->assertSame($expected, $statement->fetchCol()); $this->assertSame([], $statement->fetchCol()); } /** * Tests statement rewinding. */ Loading