在Doctrine2中将实体标记为干净

I created a simple library for Doctrine2 for managing tree structures. In repository class I created method to preload childs of tree node. My method at first download all childs of specifed node until some depth as flat array and next recreates tree structure. It works ok but I want to set preloaded childs into node insted of default not initialized PersistentCollection. When I replace PersistentCollection by my ArrayCollection with fully loaded nodes Doctrine ”thinks” that i changed relations and tries set them again after persisting.

Is there any way to mark my node again as clean to prevent Doctrine from saving relations again?

Currenty my code looks like this:

public function prepopulateTree($parent, $depth = 0)
{
    $em = $this->_em;
    $metadata = $this->getClassMetadata();

    // Retrive flat array of child nodes using closure table.
    $childs = $this->getChildHierarchyQueryBuilder($parent, false, $depth)->getQuery()->getResult();

    $recursive = function($node, $parent, $currentDepth) use (&$em, &$metadata, &$childs, &$depth, &$recursive) {
        $collection = new ArrayCollection();

        foreach ($childs as $n => $child) {
            if ($child->getParentNode()->getId() === $node->getId()) {
                $collection->add($child);
                unset($childs[$n]);
            }
        }

        $node->setParentNode($parent);
        $node->setChildNodes($collection);

        if ($depth > 0 && $currentDepth >= $depth) {
            return;
        }

        foreach ($collection as $child) {
            $recursive($child, $node, $currentDepth + 1);
        }
    };

    $recursive($parent, null, 1);

    return $parent;
}

parentNode and childNodes are declared like in this example in Doctrine documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-self-referencing