-3

In Drupal 9 I could do:

$result = \Drupal::database()->query($query, $args);

then call

$result->columnCount()

also

$recs->getColumnMeta($c)

In Drupal 10 these functions: columnCount() and getColumnMeta($c) do not exist on the \Drupal\Core\Database\StatementWrapperIterator object returned (core/lib/Drupal/Core/Database/StatementWrapperIterator.php) - or elsewhere in core.

What is best way to get this data?

Update

If I add to core/lib/Drupal/Core/Database/StatementWrapperIterator.php

  public function getColumnMeta($idx) {
    return $this->clientStatement->getColumnMeta($idx);
  }

  public function columnCount() {
    return $this->clientStatement->columnCount();
  }
  • all seems fine. Not sure if this is correct approach so figured out using getClientStatement() to get PDO functions that are missing from Drupal's wrapper.

2 Answers 2

3

It doesn't appear these were part of core in Drupal 9 either: https://api.drupal.org/api/drupal/9/search/getColumnMeta

These appear to be PHP PDO functions: https://www.php.net/manual/en/pdostatement.columncount.php https://www.php.net/manual/en/pdostatement.getcolumnmeta.php

And they still appear to exist.

1
  • Yes digging deeper I realised that. However I get: Error: Call to undefined method Drupal\Core\Database\StatementWrapperIterator::columnCount() which did not use to happen. I'd also tried the code example in the php.net with same result, so class structure has changed somehow
    – Jons
    Commented Mar 13 at 15:39
1

I'm guessing you had a custom patch in your D9 site, because even the predecessor (StatementWrapper) didn't have the methods you mention.

You shouldn't patch the core class to combat this, instead you should retrieve the PDOStatement from the wrapper, and call the method(s) on that:

$result = \Drupal::database()->query($query, $args);
$column_count = $result->getClientStatement()->columnCount();
2
  • There were no core patches but I guess something was providing the wrapper.
    – Jons
    Commented Mar 13 at 19:34
  • Thanks for your reply!
    – Jons
    Commented Mar 13 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.