Skip to content

Commit

Permalink
Merge pull request #8 from shopwareLabs/fix-level-changes-on-move
Browse files Browse the repository at this point in the history
fix level changes on move
  • Loading branch information
JanPietrzyk authored Sep 26, 2017
2 parents 400f067 + f43423f commit 92c9940
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"Shopware\\DbalNestedSet\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Shopware\\DbalNestedSetTest\\": "tests"
}
},
"require": {
"php": "^7.0",
"doctrine/dbal": "^2.5"
Expand Down
34 changes: 26 additions & 8 deletions src/NestedSetWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ public function moveAsLastChild(string $tableExpression, string $rootColumnName,
throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as last child of itself or into a descendant');
}

$level = ($parent['level'] + 1) - $child['level'];
$level = ($parent['level'] + 1);

$this->updateNodePosition($tableExpression, $child, $parent['right'], $level);
$this->updateLevel($tableExpression, $child['id'], $level);
$this->updateNodePosition($tableExpression, $child, $parent['right'], $level - $child['level']);
}

/**
Expand All @@ -233,9 +234,10 @@ public function moveAsFirstChild(string $tableExpression, string $rootColumnName
throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as first child of itself or into a descendant');
}

$level = ($parent['level'] + 1) - $child['level'];
$level = ($parent['level'] + 1);

$this->updateNodePosition($tableExpression, $child, $parent['left'] + 1, $level);
$this->updateLevel($tableExpression, $child['id'], $level);
$this->updateNodePosition($tableExpression, $child, $parent['left'] + 1, $level - $child['level']);
}

/**
Expand All @@ -259,9 +261,10 @@ public function moveAsPrevSibling(string $tableExpression, string $rootColumnNam
throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as prev sibling of itself or into a descendant');
}

$level = $sibling['level'] - $child['level'];
$level = $sibling['level'];

$this->updateNodePosition($tableExpression, $child, $sibling['left'], $level);
$this->updateLevel($tableExpression, $child['id'], $level);
$this->updateNodePosition($tableExpression, $child, $sibling['left'], $level - $child['level']);
}

/**
Expand All @@ -285,9 +288,10 @@ public function moveAsNextSibling(string $tableExpression, string $rootColumnNam
throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as next sibling of itself or into a descendant');
}

$level = $sibling['level'] - $child['level'];
$level = $sibling['level'];

$this->updateNodePosition($tableExpression, $child, $sibling['right'] + 1, $level);
$this->updateLevel($tableExpression, $child['id'], $level);
$this->updateNodePosition($tableExpression, $child, $sibling['right'] + 1, $level - $child['level']);
}

/**
Expand Down Expand Up @@ -465,4 +469,18 @@ private function applyDeltaToSubtree(string $tableExpression, int $rootValue, in
])
->execute();
}

/**
* @param string $tableExpression
* @param int $id
* @param int $level
*/
private function updateLevel(string $tableExpression, int $id, int $level)
{
$this->connection->update(
$tableExpression,
[$this->levelCol => $level],
[$this->pkCol => $id]
);
}
}
21 changes: 21 additions & 0 deletions tests/NestedSetWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ public function test_move_as_last_child()
$this->assertNode(11, 18, 19, 3, 1);
}

public function test_move_as_last_child_with_different_levels()
{
\NestedSetBootstrap::insertDemoTree();

$this->writer->moveAsLastChild('tree', 'root_id', 3, 2); //make men last child of women

\NestedSetBootstrap::validateTree(1);

$this->assertNode(1, 1, 22, 0, 1);
$this->assertNode(3, 2, 21, 1, 1);
$this->assertNode(7, 3, 8, 2, 1);
$this->assertNode(10, 4, 5, 3, 1);
$this->assertNode(11, 6, 7, 3, 1);
$this->assertNode(8, 9, 10, 2, 1);
$this->assertNode(9, 11, 12, 2, 1);
$this->assertNode(2, 13, 20, 2, 1);
$this->assertNode(4, 14, 19, 3, 1);
$this->assertNode(5, 15, 16, 4, 1);
$this->assertNode(6, 17, 18, 4, 1);
}

public function test_move_as_last_child_throws()
{
\NestedSetBootstrap::insertDemoTree();
Expand Down
10 changes: 5 additions & 5 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public static function insertDemoTree(int $rootId = 1)
$data = [
[1, 1, 22, 0, 'Clothing'],
[2, 2, 9, 1, 'Mens'],
[ 4, 3, 8, 2, 'Suits'],
[ 5, 4, 5, 3, 'Slacks'],
[ 6, 6, 7, 3, 'Jackets'],
[ 3, 10, 21, 1, 'Women'],
[ 7, 11, 16, 2, 'Dresses'],
[4, 3, 8, 2, 'Suits'],
[5, 4, 5, 3, 'Slacks'],
[6, 6, 7, 3, 'Jackets'],
[3, 10, 21, 1, 'Women'],
[7, 11, 16, 2, 'Dresses'],
[10, 12, 13, 3, 'Evening Growns'],
[11, 14, 15, 3, 'Sun Dresses'],
[8, 17, 18, 2, 'Skirts'],
Expand Down

0 comments on commit 92c9940

Please sign in to comment.