In Doctrine DBAL, how can I "stream" a query, avoid loading all the result filling the entire memory?

38 Views Asked by At

I have a very large table and I don't want to fetch all the result. I want to fetch one result at time and take the advance of PHP generators. I can't find many information about fetching a result at time, and most of the examples use old and deprecated methods.

Is the following example "right" or I'm still fetching all the records with executeQuery?

$query = ($qb = $this->connection->createQueryBuilder())
    ->select('*')
    ->from('users')
    ->where($qb->expr()->eq('enabled', ':enabled'))
    ->setParameter('enabled', true)
;

$stmt = $this->connection->executeQuery($query->getSQL(), $query->getParameters());
while ($row = $stmt->fetchAssociative()) {
    yield $row;
}
0

There are 0 best solutions below