function StatementPrefetch::current

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/StatementPrefetch.php \Drupal\Core\Database\StatementPrefetch::current()
  2. 8.9.x core/lib/Drupal/Core/Database/StatementPrefetch.php \Drupal\Core\Database\StatementPrefetch::current()

Return the current row formatted according to the current fetch style.

This is the core method of this class. It grabs the value at the current array position in $this->data and format it according to $this->fetchStyle and $this->fetchMode.

Return value

mixed The current row formatted as requested.

4 calls to StatementPrefetch::current()
StatementPrefetch::fetch in core/lib/Drupal/Core/Database/StatementPrefetch.php
Fetches the next row from a result set.
StatementPrefetch::fetchAll in core/lib/Drupal/Core/Database/StatementPrefetch.php
Returns an array containing all of the result set rows.
StatementPrefetch::fetchAllAssoc in core/lib/Drupal/Core/Database/StatementPrefetch.php
Returns the result set as an associative array keyed by the given field.
StatementPrefetch::fetchObject in core/lib/Drupal/Core/Database/StatementPrefetch.php
Fetches the next row and returns it as an object.

File

core/lib/Drupal/Core/Database/StatementPrefetch.php, line 319

Class

StatementPrefetch
An implementation of StatementInterface that pre-fetches all data.

Namespace

Drupal\Core\Database

Code

public function current() {
    if (isset($this->currentRow)) {
        switch ($this->fetchStyle) {
            case \PDO::FETCH_ASSOC:
                return $this->currentRow;
            case \PDO::FETCH_BOTH:
                // \PDO::FETCH_BOTH returns an array indexed by both the column name
                // and the column number.
                return $this->currentRow + array_values($this->currentRow);
            case \PDO::FETCH_NUM:
                return array_values($this->currentRow);
            case \PDO::FETCH_LAZY:
            // We do not do lazy as everything is fetched already. Fallback to
            // \PDO::FETCH_OBJ.
            case \PDO::FETCH_OBJ:
                return (object) $this->currentRow;
            case \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE:
                $class_name = array_shift($this->currentRow);
            // Deliberate no break.
            case \PDO::FETCH_CLASS:
                if (!isset($class_name)) {
                    $class_name = $this->fetchOptions['class'];
                }
                if (count($this->fetchOptions['constructor_args'])) {
                    // Verify the current db connection to avoid this code being called
                    // in an inappropriate context.
                    $defaults = [
                        'sqlite',
                        'oracle',
                    ];
                    $extras = Settings::get('database_statement_prefetch_valid_db_drivers', []);
                    $valid_db_drivers = array_merge($defaults, $extras);
                    $db_connection_options = Database::getConnection()->getConnectionOptions();
                    if (!in_array($db_connection_options['driver'], $valid_db_drivers)) {
                        throw new \BadMethodCallException();
                    }
                    $reflector = new \ReflectionClass($class_name);
                    $result = $reflector->newInstanceArgs($this->fetchOptions['constructor_args']);
                }
                else {
                    $result = new $class_name();
                }
                foreach ($this->currentRow as $k => $v) {
                    $result->{$k} = $v;
                }
                return $result;
            case \PDO::FETCH_INTO:
                foreach ($this->currentRow as $k => $v) {
                    $this->fetchOptions['object']->{$k} = $v;
                }
                return $this->fetchOptions['object'];
            case \PDO::FETCH_COLUMN:
                if (isset($this->columnNames[$this->fetchOptions['column']])) {
                    return $this->currentRow[$this->columnNames[$this->fetchOptions['column']]];
                }
                else {
                    return;
                }
        }
    }
}

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