Hello,

In StatementPrefetch if you are using fetchObject and pass in a class as the first parameter class_name does not get set and you get a ReflectionException.

Attached is a patch to fix it. Let me know if you guys want anything else or a test or what not.

Comments

dubcanada created an issue. See original summary.

dubcanada’s picture

StatusFileSize
new816 bytes

Patch

pifagor’s picture

I run tests

daffie’s picture

Status: Active » Needs work
Issue tags: +Needs tests

Can someone add testing for this or can post a query where this occurs.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dan.munn’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2 KB

Hi, ran into the same problem when using ultimate_cron too, agree the function signature implies you can pass the class_name for StatementPrefetch and also PDOStatement (although the StatementInterface does not).

I've updated the patch with an additional test leveraging fetchObject with a classname (although no constructor).

Status: Needs review » Needs work

The last submitted patch, 6: fetchObject_class_not_set-2956556-6.patch, failed testing. View results

dan.munn’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB

Take #2 of the above, apologies although test ran, when trying to adding a record count check clearly didn't re-run the test - updated.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

daffie’s picture

StatusFileSize
new1.23 KB

Lets test the patch with only the test in it.

daffie’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs review » Needs work
Issue tags: +Needs reroll
neslee canil pinto’s picture

Status: Needs work » Needs review
StatusFileSize
new1.3 KB

Patch Rerolled for 9.1.x

Status: Needs review » Needs work

The last submitted patch, 13: 2956556-13.patch, failed testing. View results

neslee canil pinto’s picture

Status: Needs work » Needs review
StatusFileSize
new2.1 KB

@daffie, rerolled that patch against 9.1.x

daffie’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

@Neslee: When you have done a reroll, you are supposed to remove the tag "Needs reroll". Also the added test will fail. For that I will change the status to "Needs work". Thanks for the reroll.

aleevas’s picture

Status: Needs work » Needs review
StatusFileSize
new2.11 KB
new1.1 KB

I've trued to fix last patch

daffie’s picture

@aleevas: Could be so kind to also post a patch with the test and without the fix. The patch should fail, but then we know that the test is testing the fix.

daffie’s picture

StatusFileSize
new1.32 KB

The same patch as that from comment #17 only without the fix. This way we see if the testing is testing the fix.

daffie’s picture

Status: Needs review » Needs work

The added test dos not test the bug fix.

johnwebdev’s picture

Status: Needs work » Needs review

StatementPrefetch is used by Sqlite not MySQL.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

StatementPrefetch is used by Sqlite not MySQL.

You are completely right @johndevman. Lets say that I was doing something else and I am no good at multitasking. :)

The bug fix looks good and the added test looks good.
The pacth with only the added test shows that the added test is testing the bug fix.
For me it is RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/KernelTests/Core/Database/FetchTest.php
@@ -80,6 +80,24 @@ public function testQueryFetchClass() {
+      if ($this->assertTrue($result instanceof FakeRecord, 'Result is an object of class FakeRecord.')) {
+        $this->assertIdentical($result->name, 'John', '25 year old is John.');
+      }

The if here is strange. If the assertTrue fails then the test will halt so it's not necessary.

Also instead of if ($this->assertTrue($result instanceof FakeRecord, 'Result is an object of class FakeRecord.')) { this should be

  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.');
  }

Yes this is copying other code in the class but it's all based on odd Simpletest conventions and not PHPUnit. $this->assertTrue returns NULL so amusingly none of these tests are testing what they think they are :) - can someone file a follow-up issue to fix all the tests in \Drupal\KernelTests\Core\Database\FetchTest() that are using the return value of assertTrue.

johnwebdev’s picture

Status: Needs work » Needs review
StatusFileSize
new1.25 KB
new2.05 KB
new950 bytes

Addresses #23.

johnwebdev’s picture

alexpott’s picture

+++ b/core/tests/Drupal/KernelTests/Core/Database/FetchTest.php
@@ -80,6 +80,23 @@ public function testQueryFetchClass() {
+      $records += 1;
...
+    $this->assertCount(1, $records, 'There is only one record.');

$records is not a countable thing... in other test methods $records is an array not an integer. That's why in #23 I went for $this->assertSame(1, $records, 'There is only one record.');

johnwebdev’s picture

StatusFileSize
new1.25 KB
new2.05 KB
new561 bytes

#26 Yeah, sorry about that.

The last submitted patch, 24: 2956556-24.patch, failed testing. View results

The last submitted patch, 24: 2956556-24-test-only.patch, failed testing. View results

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The code looks good.
All the points of @alexpott are addressed.
Back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed b2a1019 and pushed to 9.1.x. Thanks!

Going to ask release managers about backporting this bugfix.

  • alexpott committed b2a1019 on 9.1.x
    Issue #2956556 by johndevman, daffie, Neslee Canil Pinto, dan.munn,...
alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Patch (to be ported) » Fixed

Discussed with @catch and we agreed to backport this to 8.9.x

  • alexpott committed d88e8d2 on 8.9.x
    Issue #2956556 by johndevman, daffie, Neslee Canil Pinto, dan.munn,...

  • alexpott committed e8667fc on 9.0.x
    Issue #2956556 by johndevman, daffie, Neslee Canil Pinto, dan.munn,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.