Symfony ArrayCollection vs PersistentCollection Symfony ArrayCollection vs PersistentCollection symfony symfony

Symfony ArrayCollection vs PersistentCollection


Short answer:

/** * @param Doctrine\Common\Collections\Collection $users * @return $this */public function setChildren(Doctrine\Common\Collections\Collection $users){    $this->children = $users;    return $this;}

Explanation:

If you look deep into Doctrine Classes you will see the following structure:

Array collection is class that implements interface Collection:

class ArrayCollection implements Collection, Selectable

PersistentCollection is class that extentds AbstractLazyCollection:

final class PersistentCollection extends AbstractLazyCollection implements Selectable

but AbstractLazyCollection implements Collection:

abstract class AbstractLazyCollection implements Collection

So:

Collection is interface, that you should use in method setChildren().

This is because of doctrine use lazy loading - mechanism that allow to load not all properties, but only these, that are needed.

Similar question:

Doctrine manyToMany return PersistentCollection instead of ArrayCollection