Project

General

Profile

Actions

Bug #67144

closed

ObjectStorage->detach does not decrease positionCounter

Added by Kevin Ditscheid over 9 years ago. Updated about 5 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Extbase
Target version:
-
Start date:
2015-05-26
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
6.2
PHP Version:
5.5
Tags:
Complexity:
medium
Is Regression:
No
Sprint Focus:

Description

I encountered that if you are doing something like:

$storage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
$storage->attach(\DateTime("2015-05-21 11:38:00"));
$storage->attach(\DateTime("2015-05-22 11:38:00"));
$storage->attach(\DateTime("2015-05-26 11:38:00"));
$now = \DateTime("2015-05-25 11:38:00");

foreach($storage as $storedObject){
    if($storedObject <= $now){
        $storage->detach($storedObject);
    }
}

the positionCounter in ObjectStorage is not touched.
This is cousing the loop to jump over the next object and leaves us with an unprocessed item in the ObjectStorage, that should have been processed by the condition inside the loop, too.
The result of the code above would be:

ObjectStorage -> 
    (
        \DateTime("2015-05-22 11:38:00"),
        \DateTime("2015-05-26 11:38:00")
    )

but it should be:

ObjectStorage -> 
    (
        \DateTime("2015-05-26 11:38:00")
    )

Tested on TYPO3 6.2.13 with PHP 5.5.9

Actions #1

Updated by Kevin Ditscheid over 9 years ago

Solved it this way:

$storage->rewind();
while($storage->valid()){
    $storedObject = $storage->current();
    if($storedObject <= $now){
        $storage->detach($storedObject);
    }else{
        $storage->next();
    }
}

but i find it ugly.

Actions #2

Updated by Markus Klein over 9 years ago

  • Description updated (diff)
Actions #3

Updated by Markus Klein over 9 years ago

  • Status changed from New to Needs Feedback

The positionCounter is not involved here. The foreach loop uses the \Iterator interface.

Seems like removing am element somehow distorts the internal array pointer of PHP on the $storage array.
The \Iterator methods just use the regular next(), current(), etc. functions on the internal array.

Actions #4

Updated by Alexander Opitz about 9 years ago

  • Status changed from Needs Feedback to Closed

No feedback within the last 90 days => closing this issue.

If you think that this is the wrong decision or experience this issue again, then please write to the mailing list typo3.teams.bugs with issue number and an explanation or open a new ticket and add a relation to this ticket number.

Actions #5

Updated by Michal Cygankiewicz about 5 years ago

I have same issue in TYPO3 8.7.27

Actions #6

Updated by Kevin Ditscheid about 5 years ago

Michal Cygankiewicz wrote:

I have same issue in TYPO3 8.7.27

My view on this changed over time. It is generally not good advice to remove elements from lists while looping over them.
Just create a new List, put stuff into this new List and return it instead of the old one.

Actions

Also available in: Atom PDF