在Doctrine2中将实体标记为干净

时间:2014-12-15 09:46:42

标签: php doctrine-orm

我为Doctrine2创建了一个简单的库来管理树结构。在存储库类中,我创建了预加载树节点子节点的方法。我的方法首先下载指定节点的所有子节点,直到某个深度为平面数组,然后重新创建树结构。它工作正常,但我想将预加载的子节点设置为默认未初始化PersistentCollection的节点。当我用PersistentCollection用满载节点替换ArrayCollection时,Doctrine“认为”我改变了关系,并在持久化后再尝试设置它们。

有没有办法再次将我的节点标记为干净以防止Doctrine再次保存关系?

Currenty我的代码看起来像这样:

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和childNodes在Doctrine文档中的示例中声明:http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-self-referencing

0 个答案:

没有答案
相关问题