Issue #3128548 by anmolgoyal74, yonailo, sokru, joelpittet, daffie, johnwebdev, alexpott, mradcliffe, DuneBL, catch, xjm, mondrake: Add optional parameters to StatementInterface::fetchObject() to be in line with the PDO implementation of the method fetchObject()

merge-requests/4973/head
Alex Pott 2021-02-08 15:53:53 +00:00
parent 2b44c55741
commit cc41d94d0f
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
6 changed files with 41 additions and 8 deletions

View File

@ -68,7 +68,7 @@ class StatementEmpty implements \Iterator, StatementInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchObject() { public function fetchObject(string $class_name = NULL, array $constructor_arguments = NULL) {
return NULL; return NULL;
} }

View File

@ -127,8 +127,21 @@ interface StatementInterface extends \Traversable {
* *
* The object will be of the class specified by StatementInterface::setFetchMode() * The object will be of the class specified by StatementInterface::setFetchMode()
* or stdClass if not specified. * or stdClass if not specified.
*
* phpcs:disable Drupal.Commenting
* @todo Remove PHPCS overrides https://www.drupal.org/node/3194677.
*
* @param string|null $class_name
* Name of the created class.
* @param array|null $constructor_arguments
* Elements of this array are passed to the constructor.
* phpcs:enable
*
* @return mixed
* The object of specified class or \stdClass if not specified. Returns
* FALSE or NULL if there is no next row.
*/ */
public function fetchObject(); public function fetchObject(/* string $class_name = NULL, array $constructor_arguments = NULL */);
/** /**
* Fetches the next row and returns it as an associative array. * Fetches the next row and returns it as an associative array.

View File

@ -418,7 +418,7 @@ class StatementPrefetch implements \Iterator, StatementInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchObject($class_name = NULL, $constructor_args = []) { public function fetchObject(string $class_name = NULL, array $constructor_arguments = NULL) {
if (isset($this->currentRow)) { if (isset($this->currentRow)) {
if (!isset($class_name)) { if (!isset($class_name)) {
// Directly cast to an object to avoid a function call. // Directly cast to an object to avoid a function call.
@ -428,7 +428,7 @@ class StatementPrefetch implements \Iterator, StatementInterface {
$this->fetchStyle = \PDO::FETCH_CLASS; $this->fetchStyle = \PDO::FETCH_CLASS;
$this->fetchOptions = [ $this->fetchOptions = [
'class' => $class_name, 'class' => $class_name,
'constructor_args' => $constructor_args, 'constructor_args' => $constructor_arguments,
]; ];
// Grab the row in the format specified above. // Grab the row in the format specified above.
$result = $this->current(); $result = $this->current();

View File

@ -190,9 +190,9 @@ class StatementWrapper implements \IteratorAggregate, StatementInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchObject(string $class_name = NULL) { public function fetchObject(string $class_name = NULL, array $constructor_arguments = NULL) {
if ($class_name) { if ($class_name) {
return $this->clientStatement->fetchObject($class_name); return $this->clientStatement->fetchObject($class_name, $constructor_arguments);
} }
return $this->clientStatement->fetchObject(); return $this->clientStatement->fetchObject();
} }

View File

@ -9,4 +9,23 @@ namespace Drupal\Tests\system\Functional\Database;
* rather than just a stdClass or array. This class is for testing that * rather than just a stdClass or array. This class is for testing that
* functionality. (See testQueryFetchClass() below) * functionality. (See testQueryFetchClass() below)
*/ */
class FakeRecord {} class FakeRecord {
/**
* A class variable.
*
* @var int
*/
public $fakeArg;
/**
* Constructs a FakeRecord object with an optional constructor argument.
*
* @param int $fakeArg
* A class variable.
*/
public function __construct($fakeArg = 0) {
$this->fakeArg = $fakeArg;
}
}

View File

@ -88,10 +88,11 @@ class FetchTest extends DatabaseTestBase {
public function testQueryFetchObjectClass() { public function testQueryFetchObjectClass() {
$records = 0; $records = 0;
$query = $this->connection->query('SELECT [name] FROM {test} WHERE [age] = :age', [':age' => 25]); $query = $this->connection->query('SELECT [name] FROM {test} WHERE [age] = :age', [':age' => 25]);
while ($result = $query->fetchObject(FakeRecord::class)) { while ($result = $query->fetchObject(FakeRecord::class, [1])) {
$records += 1; $records += 1;
$this->assertInstanceOf(FakeRecord::class, $result); $this->assertInstanceOf(FakeRecord::class, $result);
$this->assertSame('John', $result->name, '25 year old is John.'); $this->assertSame('John', $result->name, '25 year old is John.');
$this->assertSame(1, $result->fakeArg, 'The record has received an argument through its constructor.');
} }
$this->assertSame(1, $records, 'There is only one record.'); $this->assertSame(1, $records, 'There is only one record.');
} }