4

I'm looking for a working solution, to iterate over a PersistentCollection in . Unfortunately this seems not to work? Symfony ignores the next() function!

while (($animal = $zooAnimals->next()) !== false) {

    $color = $animal->getColor();

    print_r($color); die; // Test and die
}

print_r('Where are the animals?'); die; // << Current result

Reference: Doctrine\ODM\MongoDB\PersistentCollection

4
  • What's wrong with foreach($zooAnimals as $animal) ?
    – Touki
    Commented Dec 19, 2013 at 13:11
  • @Touki because $zooAnimals is not a array()! is_array($zooAnimals); // false
    – mate64
    Commented Dec 19, 2013 at 13:16
  • 1
    So what? Your collection implements Doctrine\Common\Collection which implements IteratorAggregate which lets you iterate (foreach) over an object.
    – Touki
    Commented Dec 19, 2013 at 13:18
  • Yes, you're right. Thank you for the enlightening answer below !
    – mate64
    Commented Dec 19, 2013 at 16:36

4 Answers 4

10

This is not Symfony's "fault". This is a misunderstanding of how to iterate over an object. There are several ways to handle this for your use case. Here are some

Use a foreach!

Your PersistentCollection implements Collection which implements IteratorAggregate which implements Traversable (long way heh?).
An object which implements interface Traversable can be used in a foreach statement.

IteratorAggregate forces you to implement one method getIterator which must return an Iterator. This last also implements Traversable interface.

Usage of an iterator

Iterator interface forces your object to declare 5 methods in order to be used by a foreach

class MyCollection implements Iterator
{
    protected $parameters = array();
    protected $pointer = 0;

    public function add($parameter)
    {
        $this->parameters[] = $parameter;
    }

    /**
     * These methods are needed by Iterator
     */
    public function current()
    {
        return $this->parameters[$this->pointer];
    }

    public function key()
    {
        return $this->pointer;
    }

    public function next()
    {
        $this->pointer++;
    }

    public function rewind()
    {
        $this->pointer = 0;
    }

    public function valid()
    {
        return array_key_exists($this->pointer, $this->parameters);
    }
}

You can use any class which implements Iterator it like this - Demo file

$coll = new MyCollection;
$coll->add('foo');
$coll->add('bar');

foreach ($coll as $key => $parameter) {
    echo $key, ' => ', $parameter, PHP_EOL;
}

Use iterator with a while

In order to use this class like a foreach. Methods should be called this way - Demo file

$coll->rewind();

while ($coll->valid()) {
    echo $coll->key(), ' => ', $coll->current(), PHP_EOL;
    $coll->next();
}
0
3

Simple solution:

1 Convert your PersistentCollection to an array first

$zooAnimalsArray = $zooAnimals->toArray();

2 Handle the array classically like you would with any PHP arrays.

Note This has the advantage of creating code that doesn't depend too much on your database (in case you wish one day to switch to a relational database), you wouldn't have to rewrite everything.

2

Works to me!

$collection = new ArrayCollection();
$collection->add('Laranja');
$collection->add('Uva');
$collection->add('Morango');

do {
    print_r($collection->current());
} while ($collection->next());
0

This is my solution,

$zooAnimals->getIterator();

while ($animal = $zooAnimals->current()) {

    echo $animal->getColor();

    $zooAnimals->next();
}
3
  • 1
    Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Commented Sep 15, 2020 at 14:30
  • With getIterator() gets a Iterator to iterate over the array of objects, then with while loops over the array thanks the Iterator, current()get the object of the current position, finally with next() you get next object of the array.
    – Ivan Moll
    Commented Mar 28, 2021 at 19:55
  • Please edit your answer to contain all explanation
    – Nico Haase
    Commented Mar 29, 2021 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.