Illuminate\Database\Connection::cursor PHP Method

cursor() public method

Run a select statement against the database and returns a generator.
public cursor ( string $query, array $bindings = [], boolean $useReadPdo = true ) : Generator
$query string
$bindings array
$useReadPdo boolean
return Generator
    public function cursor($query, $bindings = [], $useReadPdo = true)
    {
        $statement = $this->run($query, $bindings, function ($me, $query, $bindings) use($useReadPdo) {
            if ($me->pretending()) {
                return [];
            }
            $statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
            $fetchMode = $me->getFetchMode();
            $fetchArgument = $me->getFetchArgument();
            $fetchConstructorArgument = $me->getFetchConstructorArgument();
            if ($fetchMode === PDO::FETCH_CLASS && !isset($fetchArgument)) {
                $fetchArgument = 'StdClass';
                $fetchConstructorArgument = null;
            }
            if (isset($fetchArgument)) {
                $statement->setFetchMode($fetchMode, $fetchArgument, $fetchConstructorArgument);
            } else {
                $statement->setFetchMode($fetchMode);
            }
            $me->bindValues($statement, $me->prepareBindings($bindings));
            $statement->execute();
            return $statement;
        });
        while ($record = $statement->fetch()) {
            (yield $record);
        }
    }

Usage Example

示例#1
0
 /**
  * Get a generator for the given query.
  *
  * @return \Generator
  */
 public function cursor()
 {
     if (is_null($this->columns)) {
         $this->columns = ['*'];
     }
     return $this->connection->cursor($this->toSql(), $this->getBindings(), !$this->useWritePdo);
 }