DEV Community

Cover image for PersistentCollection on Doctrine ORM in Symfony
David Maye
David Maye

Posted on Edited on

PersistentCollection on Doctrine ORM in Symfony

While working on my Modular ERP SystemBundle (the bundle that is responsible to display admin panel and dashboards), I thought that on Doctrine Entities ManyToOne Collections where simple arrays. It was very naive of me considering that I wrote a code that returned a Collection earlier.

It seems that when an entity relationship object is fetched, all the objects are not fetched at all. Only a reference to the Collection (ArrayCollection, similar to an array) is delivered to futher code manipulation. It seems that this is what PersistentCollection is: A lazy collection of element that has the hability to get initialized (object loaded) and get it's snapshot (array of it objects). The lazy side is my deduction: when I tried to get the snapshot of the object using the function getSnapshot(), it returned and empty array. However, when I first called the initialize() method and then the getSnapshot() method, the array was filled with the entities of the collection. I think this is due to the fact that initialize() method invoke the EntityManager to get the details in the database. I share the codes bellow:

    /**
     * @return array<array-key, null|object>|null
     */
    #[\Override]
    public function getCurrentEntities(): ?array
    {
        $entities = null;
        $parentEntities = parent::getCurrentEntities();
        if(!is_null($parentEntities[0]))
        {
            $entity = $parentEntities[0];
            if(!is_null($entity) && $entity instanceof User)
            {
                $entities = $entity->getSystemGroupUsers();
                $entities->initialize();
                $entities = $entities->getSnapshot();
            }
        }
        return $entities;
    }
Enter fullscreen mode Exit fullscreen mode

initialize() method of the PersistentCollection class.

    public function initialize()
    {
        if ( ! $this->initialized && $this->association) {
            if ($this->isDirty) {
                // Has NEW objects added through add(). Remember them.
                $newObjects = $this->coll->toArray();
            }
            $this->coll->clear();
            $this->em->getUnitOfWork()->loadCollection($this);
            $this->takeSnapshot();
            // Reattach NEW objects added through add(), if any.
            if (isset($newObjects)) {
                foreach ($newObjects as $obj) {
                    $this->coll->add($obj);
                }
                $this->isDirty = true;
            }
            $this->initialized = true;
        }
    }
Enter fullscreen mode Exit fullscreen mode

takeSnapshot() method of the PersistentCollection class.

    public function takeSnapshot()
    {
        $this->snapshot = $this->coll->toArray();
        $this->isDirty = false;
    }

Enter fullscreen mode Exit fullscreen mode

getSnapshot() method of the PersistentCollection class.

    public function getSnapshot()
    {
        return $this->snapshot;
    }

Enter fullscreen mode Exit fullscreen mode

The lazy behaviour is real. Although I'm not sure how it works underneath. I would have to identify how the EntityManager class works, especially the line $this->em->getUnitOfWork()->loadCollection($this);. Although it is not important as for now. Just a curiousity.

Edit: I'm curious on how the collection passes to a PersistentCollection. However, it is simpler to just use the toArray() method as the following:

    /**
     * @return array<array-key, null|object>|null
     */
    #[\Override]
    public function getCurrentEntities(): ?array
    {
        $entities = null;
        $parentEntities = parent::getCurrentEntities();
        $entity = null;

        if(!is_null($parentEntities))
        {
            $entity = $parentEntities[0];
            if (!is_null($entity) && $entity instanceof SystemGroup) {
                $entities = $entity->getSystemGroupUsers()->toArray();
            }
        }

        return $entities;
    }
Enter fullscreen mode Exit fullscreen mode

Kind regards,
David Maye.

Top comments (0)