Iterate undefined offset 0 #5037

Closed
opened 2026-01-22 14:57:06 +01:00 by admin · 9 comments
Owner

Originally created by @iskyd on GitHub (Mar 7, 2016).

From my function i return by a native query and iterator :
return $q->iterate();
Now in accordance with documentation :

if ($iterator) {
            while (($row = $iterator->next()) !== false) {
                $var = $row[0];
                var_dump($var);
                $em->clear();
            }
}

But after the first execution i get :

Undefined offset 0

If i dump $row variable i notice that the first execution i get from $row[0] the second $row[1] and so on...

Originally created by @iskyd on GitHub (Mar 7, 2016). From my function i return by a native query and iterator : `return $q->iterate();` Now in accordance with documentation : ``` if ($iterator) { while (($row = $iterator->next()) !== false) { $var = $row[0]; var_dump($var); $em->clear(); } } ``` But after the first execution i get : > Undefined offset 0 If i dump $row variable i notice that the first execution i get from $row[0] the second $row[1] and so on...
admin closed this issue 2026-01-22 14:57:06 +01:00
Author
Owner

@ghost commented on GitHub (Sep 23, 2016):

I think you're right, because I noticed the same. For others having this issue: You could try $iterator->key() for the currently valid index of $row.

But it would be more intuitive, if $row would already contain the correct value, instead of an array, imo (maybe there are cases idk about yet which make it necessary).

@ghost commented on GitHub (Sep 23, 2016): I think you're right, because I noticed the same. For others having this issue: You could try `$iterator->key()` for the currently valid index of `$row`. But it would be more intuitive, if `$row` would already contain the correct value, instead of an array, imo (maybe there are cases idk about yet which make it necessary).
Author
Owner

@flack commented on GitHub (Oct 5, 2017):

I just stumbled across this, too: It happens if your hydration mode is not HYDRATE_OBJECT, i.e. if you iterate over objects, it works as documented, but for e.g. array results, you'll get this error

@flack commented on GitHub (Oct 5, 2017): I just stumbled across this, too: It happens if your hydration mode is not `HYDRATE_OBJECT`, i.e. if you iterate over objects, it works as documented, but for e.g. array results, you'll get this error
Author
Owner

@flack commented on GitHub (Oct 5, 2017):

Seems to be the same as #3238.

@Ocramius is this intended behavior?

@flack commented on GitHub (Oct 5, 2017): Seems to be the same as #3238. @Ocramius is this intended behavior?
Author
Owner

@lcobucci commented on GitHub (Nov 26, 2017):

@iskyd @whatda @flack could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing.

You can find examples on 388afb46d0/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (Nov 26, 2017): @iskyd @whatda @flack could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing. You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@flack commented on GitHub (Nov 26, 2017):

@lcobucci I can make a unittest if you want, but it's really very simple. The code to reproduce is right there in the ticket description

@flack commented on GitHub (Nov 26, 2017): @lcobucci I can make a unittest if you want, but it's really very simple. The code to reproduce is right there in the ticket description
Author
Owner

@lcobucci commented on GitHub (Nov 26, 2017):

@flack so, please, send us a PR 👍

@lcobucci commented on GitHub (Nov 26, 2017): @flack so, please, send us a PR 👍
Author
Owner

@Steveb-p commented on GitHub (Jan 8, 2018):

I've stumbled across this as well with exactly the same situation. iterate works alright if you're using object hydration, and at the point when you start using arrays (for me I just needed the ID) iterate starts using 0,1,2,3... as keys.

Question is it this intended behavior. For people already going around this issue it will be kind of a BC break.

@Steveb-p commented on GitHub (Jan 8, 2018): I've stumbled across this as well with exactly the same situation. `iterate` works alright if you're using object hydration, and at the point when you start using arrays (for me I just needed the ID) `iterate` starts using 0,1,2,3... as keys. Question is it this intended behavior. For people already going around this issue it will be kind of a BC break.
Author
Owner

@peter-gribanov commented on GitHub (Feb 1, 2019):

I solve this problem using the current() function. But i agree. This is a very strange behavior.

public function iterate(): iterable
{
    foreach ($this->rep->createQueryBuilder('e')->getQuery()->iterate() as $row) {
        yield current($row);
    }
}
@peter-gribanov commented on GitHub (Feb 1, 2019): I solve this problem using the `current()` function. But i agree. This is a very strange behavior. ```php public function iterate(): iterable { foreach ($this->rep->createQueryBuilder('e')->getQuery()->iterate() as $row) { yield current($row); } } ```
Author
Owner

@beberlei commented on GitHub (Dec 5, 2020):

This is fixed by moving to the new method Query::toIterable in 2.8

@beberlei commented on GitHub (Dec 5, 2020): This is fixed by moving to the new method `Query::toIterable` in 2.8
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5037