Skip to content

Conversation

@carloscarucce
Copy link

This method creates a generator that will fetch one row at a time from the data set.
This aims to reduce memory load during ResultSet interation while maintaining the simplicity of "fetchAll" when using it inside a loop.

Here is an example:

$rows = $yourQuery->lazy(PDO::FETCH_ASSOC);

foreach ($rows as $row) {
    echo $row['name'], ' - ', $row['age'];
}

Hope you find it useful

@sorinsarca
Copy link
Member

Hi @carloscarucce

This should probably implemented using existing API:

  • use IteratorAggregate on class ResultSet
  • use existing fetch*() methods, like fetchAssoc()
class ResultSet implements \IteratorAggregate {
  // Rest of the class ...

  public function getIterator(): \Generator
  {
     const statement = $this->statement;
     try {
       while (false !== ($row = $statement->fetch())) {
         yield $row;
       }
     } finally {
       $statement->closeCursor();
     }
  }
}

And can be directly used by foreach:

$result = $db->from('users')
             ->select(['name', 'email'])
             ->fetchAssoc();

foreach ($result as $user) {
  echo $user['name'], $user['email'], "\n";
}

@carloscarucce
Copy link
Author

Hey @sorinsarca, hope you're doing well

That would make every result set iterate over a lazy source. Which would not be the optimal in some cases (where people invoke fetchAll, for instance).

Making it a separate method gives the user the control over the component so it is used only when it's needed.

@sorinsarca
Copy link
Member

no, by implementing IteratorAggregate doesn't mean we get rid of the other methods like all() or first().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants