Symfony ArrayCollection vs PersistentCollection

后端 未结 1 791
忘了有多久
忘了有多久 2021-02-04 02:50

As I understood when you query database by repository you get PersistentCollection and when your working with your entities you get ArrayCollection.

so consider I have o

相关标签:
1条回答
  • 2021-02-04 03:01

    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

    0 讨论(0)
提交回复
热议问题