I\'ve got a basic codeset like this (inside a controller):
$sql = \'select * from someLargeTable limit 1000\';
$em = $this->getDoctrine()->getManager();
$c
The selected answer is wrong and @kroky's answer should be selected as the correct one.
The problem is Buffer vs Unbuffered Queries.
Now it won't be a good idea to change the behaviour for all queries, because:
Unless the full result set was fetched from the server no further queries can be sent over the same connection.
Hence, it should only be used when necessary. Here is a full working example with >200k objects:
$qb = ...->createQueryBuilder('p');
$this
->em
->getConnection()
->getWrappedConnection()
->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$query = $qb->getQuery();
$result = $query->iterate();
$batchSize = 20;
$i = 0;
foreach ($result as $product)
{
$i++;
var_dump($product[0]->getSku());
if (($i % $batchSize) === 0) {
$this->em->flush();
$this->em->clear(); // Detaches all objects from Doctrine!
}
}
It most likely needs some refinement.