function StatementBase::fetchObject

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Statement/StatementBase.php \Drupal\Core\Database\Statement\StatementBase::fetchObject()

Fetches the next row and returns it as an object.

The object will be of the class specified by StatementInterface::setFetchMode() or stdClass if not specified.

Parameters

string|null $class_name: (Optional) Name of the created class.

array $constructor_arguments: (Optional) Elements of this array are passed to the constructor.

Return value

object|false|null The object of specified class or \stdClass if not specified. Returns FALSE or NULL if there is no next row.

Overrides StatementInterface::fetchObject

File

core/lib/Drupal/Core/Database/Statement/StatementBase.php, line 256

Class

StatementBase
StatementInterface base implementation.

Namespace

Drupal\Core\Database\Statement

Code

public function fetchObject(?string $className = NULL, array $constructorArguments = []) {
  $row = $className === NULL ? $this->result
    ->fetch(FetchAs::Object, []) : $this->result
    ->fetch(FetchAs::ClassObject, [
    'class' => $className,
    'constructor_args' => $constructorArguments,
  ]);
  if ($row === FALSE) {
    $this->markResultsetFetchingComplete();
    return FALSE;
  }
  $this->setResultsetCurrentRow($row);
  return $row;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.