function StatementBase::setFetchMode

Sets the default fetch mode for this statement.

Parameters

\Drupal\Core\Database\Statement\FetchAs|int $mode: One of the cases of the FetchAs enum, or (deprecated) a \PDO::FETCH_* constant.

string|int|null $a1: An option depending of the fetch mode specified by $mode:

  • for FetchAs::Column, the index of the column to fetch;
  • for FetchAs::ClassObject, the name of the class to create.

list<mixed> $a2: If $mode is FetchAs::ClassObject, the optional arguments to pass to the

  • for \PDO::FETCH_COLUMN, the index of the column to fetch.
  • for \PDO::FETCH_CLASS, the name of the class to create.
  • for \PDO::FETCH_INTO, the object to add the data to.

constructor.

Return value

bool TRUE if successful, FALSE if not.

Overrides StatementInterface::setFetchMode

5 calls to StatementBase::setFetchMode()
Statement::execute in core/modules/mysqli/src/Driver/Database/mysqli/Statement.php
Executes a prepared statement.
Statement::__construct in core/modules/mysqli/src/Driver/Database/mysqli/Statement.php
Constructs a Statement object.
StatementPrefetchIterator::execute in core/lib/Drupal/Core/Database/StatementPrefetchIterator.php
Executes a prepared statement.
StatementWrapperIterator::execute in core/lib/Drupal/Core/Database/StatementWrapperIterator.php
Executes a prepared statement.
StatementWrapperIterator::__construct in core/lib/Drupal/Core/Database/StatementWrapperIterator.php
Constructs a StatementWrapperIterator object.

File

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

Class

StatementBase
StatementInterface base implementation.

Namespace

Drupal\Core\Database\Statement

Code

public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
  if (is_int($mode)) {
    @trigger_error("Passing the \$mode argument as an integer to setFetchMode() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\Statement\\FetchAs enum instead. See https://www.drupal.org/node/3488338", E_USER_DEPRECATED);
    $mode = $this->pdoToFetchAs($mode);
  }
  assert($mode instanceof FetchAs);
  $this->fetchMode = $mode;
  switch ($mode) {
    case FetchAs::ClassObject:
      $this->fetchOptions['class'] = $a1;
      if ($a2) {
        $this->fetchOptions['constructor_args'] = $a2;
      }
      break;

    case FetchAs::Column:
      $this->fetchOptions['column'] = $a1;
      break;

  }
  // If the result object is missing, just do with the properties setting.
  try {
    if ($this->result) {
      return $this->result
        ->setFetchMode($mode, $this->fetchOptions);
    }
    return TRUE;
  } catch (\LogicException) {
    return TRUE;
  }
}

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