Loading core/lib/Drupal/Core/Database/StatementPrefetch.php +4 −1 Original line number Diff line number Diff line Loading @@ -417,7 +417,10 @@ public function fetchObject($class_name = NULL, $constructor_args = []) { } else { $this->fetchStyle = \PDO::FETCH_CLASS; $this->fetchOptions = ['constructor_args' => $constructor_args]; $this->fetchOptions = [ 'class' => $class_name, 'constructor_args' => $constructor_args, ]; // Grab the row in the format specified above. $result = $this->current(); // Reset the fetch parameters to the value stored using setFetchMode(). Loading core/tests/Drupal/KernelTests/Core/Database/FetchTest.php +17 −0 Original line number Diff line number Diff line Loading @@ -80,6 +80,23 @@ public function testQueryFetchClass() { $this->assertIdentical(count($records), 1, 'There is only one record.'); } /** * Confirms that we can fetch a record into a class using fetchObject. * * @see \Drupal\system\Tests\Database\FakeRecord * @see \Drupal\Core\Database\StatementPrefech::fetchObject */ public function testQueryFetchObjectClass() { $records = 0; $query = $this->connection->query('SELECT name FROM {test} WHERE age = :age', [':age' => 25]); while ($result = $query->fetchObject(FakeRecord::class)) { $records += 1; $this->assertInstanceOf(FakeRecord::class, $result); $this->assertSame('John', $result->name, '25 year old is John.'); } $this->assertSame(1, $records, 'There is only one record.'); } /** * Confirms that we can fetch a record into a new instance of a custom class. * The name of the class is determined from a value of the first column. Loading Loading
core/lib/Drupal/Core/Database/StatementPrefetch.php +4 −1 Original line number Diff line number Diff line Loading @@ -417,7 +417,10 @@ public function fetchObject($class_name = NULL, $constructor_args = []) { } else { $this->fetchStyle = \PDO::FETCH_CLASS; $this->fetchOptions = ['constructor_args' => $constructor_args]; $this->fetchOptions = [ 'class' => $class_name, 'constructor_args' => $constructor_args, ]; // Grab the row in the format specified above. $result = $this->current(); // Reset the fetch parameters to the value stored using setFetchMode(). Loading
core/tests/Drupal/KernelTests/Core/Database/FetchTest.php +17 −0 Original line number Diff line number Diff line Loading @@ -80,6 +80,23 @@ public function testQueryFetchClass() { $this->assertIdentical(count($records), 1, 'There is only one record.'); } /** * Confirms that we can fetch a record into a class using fetchObject. * * @see \Drupal\system\Tests\Database\FakeRecord * @see \Drupal\Core\Database\StatementPrefech::fetchObject */ public function testQueryFetchObjectClass() { $records = 0; $query = $this->connection->query('SELECT name FROM {test} WHERE age = :age', [':age' => 25]); while ($result = $query->fetchObject(FakeRecord::class)) { $records += 1; $this->assertInstanceOf(FakeRecord::class, $result); $this->assertSame('John', $result->name, '25 year old is John.'); } $this->assertSame(1, $records, 'There is only one record.'); } /** * Confirms that we can fetch a record into a new instance of a custom class. * The name of the class is determined from a value of the first column. Loading