DDC-3929: Iterating results documentation #4807

Closed
opened 2026-01-22 14:49:46 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 1, 2015).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ruscon:

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#id1

<?php
$batchSize = 20;
$i = 0; // <- BUG. if $i = 0, then you have commit after first iteration :)
$q = $em->createQuery('select u from MyProject\Model\User u');
$iterableResult = $q->iterate();
while (($row = $iterableResult->next()) !== false) {
    $em->remove($row[0]);
    if (($i % $batchSize) === 0) {
        $em->flush(); // Executes all deletions.
        $em->clear(); // Detaches all objects from Doctrine!
    }
    <ins></ins>$i;
}
$em->flush();
Originally created by @doctrinebot on GitHub (Oct 1, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ruscon: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#id1 ``` java <?php $batchSize = 20; $i = 0; // <- BUG. if $i = 0, then you have commit after first iteration :) $q = $em->createQuery('select u from MyProject\Model\User u'); $iterableResult = $q->iterate(); while (($row = $iterableResult->next()) !== false) { $em->remove($row[0]); if (($i % $batchSize) === 0) { $em->flush(); // Executes all deletions. $em->clear(); // Detaches all objects from Doctrine! } <ins></ins>$i; } $em->flush(); ```
admin added the Bug label 2026-01-22 14:49:46 +01:00
admin closed this issue 2026-01-22 14:49:47 +01:00
Author
Owner

@vctls commented on GitHub (Dec 11, 2019):

I suppose the corresponding doc link is this one:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/batch-processing.html#iterating-large-results-for-data-processing

detach is deprecated, but clear does not allow to detach only one object. I tried to use clear instead of detach($object) in one of my iterating scripts and it seemed to work, but I have to check for side effects.

@vctls commented on GitHub (Dec 11, 2019): I suppose the corresponding doc link is this one: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/batch-processing.html#iterating-large-results-for-data-processing `detach` is deprecated, but `clear` does not allow to detach only one object. I tried to use `clear` instead of `detach($object)` in one of my iterating scripts and it seemed to work, but I have to check for side effects.
Author
Owner

@SenseException commented on GitHub (Dec 16, 2019):

The original issue was already fixed. Thank you for pointing that out and bringing more context into this issue, @vctls 👍

@SenseException commented on GitHub (Dec 16, 2019): The original issue was already fixed. Thank you for pointing that out and bringing more context into this issue, @vctls :+1:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4807