Problem/Motivation

StatementPrefetch::current() contains this snippet:

case \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE:
          $class_name = array_unshift($this->currentRow);

That is syntactically wrong, as array_unshift() takes 2 arguments and returns a new array. It probably should be array_shift().

It wasn't caught because FETCH_CLASS is apparently the only fetch mode that 1) We don't test properly and 2) We don't actually use in core. :-) Also, StatementPrefetch is only used by SQLite, which until recently wasn't even tested.

Proposed resolution

1) Fix the bug.
2) Add a test to

https://api.drupal.org/api/drupal/core%21modules%21system%21src%21Tests%...

for the missing fetch modes.

CommentFileSizeAuthor
#4 fix-bug-fetch-class-2499875-4.patch1.9 KBa_thakur
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cilefen’s picture

Is the title for this correct? It seems like a code bug, not a documentation style bug.

Crell’s picture

Title: Fix documentation style in core/lib/Drupal/Core/Database/StatementPrefetch.php » Fix bug with prefetch drivers and FETCH_CLASS

Oops. Yes, I did a clone issue and forgot to change that.

a_thakur’s picture

Assigned: Unassigned » a_thakur

Working on this.

a_thakur’s picture

Status: Needs work » Needs review
FileSize
1.9 KB

* Replaced array_unshift() with array_shift().
* Wrote test case for FETCH_LAZY
* Test cases for FETCH_INTO and \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE are remaining. Any help regarding these would be great.

Crell’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/src/Tests/Database/FetchTest.php
@@ -34,6 +34,21 @@ function testQueryFetchDefault() {
+  /**
+   * Confirms that we can fetch a record lazily.
+   */
+  function testQueryFetchLazy() {

Thanks. It's not object fetch that isn't being tested, though. It's CLASS fetch. Ie, loading a record directly into a PHP class. Check the docs for PDO::FETCH_CLASS. (I don't know what FETCH_CLASSTYPE does, honestly.)

a_thakur’s picture

Assigned: a_thakur » Unassigned
Berdir’s picture