PDOStatement::fetchObject() expects parameter 2 to be array, null given in Drupal\Core\Database\StatementWrapper->fetchObject()

Created on 21 January 2022, almost 3 years ago
Updated 18 September 2023, over 1 year ago

Problem/Motivation

When calling fetchObject with a class name but without supplying constructor arguments, we get this warning:

Warning: PDOStatement::fetchObject() expects parameter 2 to be array, null given in Drupal\Core\Database\StatementWrapper->fetchObject()

This is because StatementWrapper defaults this to NULL, but PDOStatement expects an empty array if the argument is supplied.

Steps to reproduce

final class MyDatabaseObjectClass {
}

$queryResult = \Drupal::database()
  ->select('key_value', 'kv')
  ->fields('kv', ['name'])
  ->condition('collection', 'system.schema')
  ->condition('name', 'node')->execute();

$resultRowAsObject = $queryResult->fetchObject(MyDatabaseObjectClass::class);

Expected: An instance of MyDatabaseObjectClass
Actual: FALSE, plus a PHP Warning

Proposed resolution

Either the method signature of StatementWrapper::fetchObject should be changed to more closely align with PDOStatement::fetchObject, or a NULL guard should be added when passing $constructor_arguments to ensure an empty array is passed instead of NULL.

Release notes snippet

Any contrib database driver projects that implement StatementInterface::fetchObject will need to adjust their signature accordingly to prevent similar fatal errors.

๐Ÿ› Bug report
Status

Fixed

Version

11.0 ๐Ÿ”ฅ

Component
Databaseย  โ†’

Last updated 2 days ago

  • Maintained by
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands @daffie
Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom pstewart

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024