0

I created a controller action to reorder the nodes of a nested tree branch. I took example on the DoctrineExtensions documentation, but the code is actually trashing the tree. What am I doing wrong?

The code of my controller action is as follows:

public function orderUpdateAction($id)
{
    $order = $this->request->getPost()['order'];
    $parent = $this->getLocationById($id);
    $repository = $this->getRepository();
    $previous = null;

    foreach ($order as $childId) {
        $child = $this->getLocationById($childId);

        if (!$child) {
            throw new \InvalidArgumentException("Unknown location: $childId");
        }

        if ($previous) {
            $repository->persistAsNextSiblingOf($child, $previous);
        } else {
            $repository->persistAsFirstChildOf($child, $parent);
        }

        $previous = $child;
    }

    $this->entitymanager->flush();

    // …
}

verify() returns the following errors:

array (size=8)
  0 => string 'index [1], duplicate on tree root: 1' (length=36)
  1 => string 'index [2], duplicate on tree root: 1' (length=36)
  2 => string 'index [3], duplicate on tree root: 1' (length=36)
  3 => string 'index [4], missing on tree root: 1' (length=34)
  4 => string 'index [5], missing on tree root: 1' (length=34)
  5 => string 'index [6], missing on tree root: 1' (length=34)
  6 => string 'index [7], missing on tree root: 1' (length=34)
  7 => string 'node [3] has invalid left or right values' (length=41)
olvlvl
  • 2,396
  • 1
  • 22
  • 22

1 Answers1

1

You should to move $this->entitymanager->flush(); to foreach loop.

Prazmok666
  • 26
  • 1
  • I have no way to check if it works now (old project), still I'm assuming your answer is valid. Thanks for answering my question. – olvlvl Oct 07 '15 at 10:48