Skip to content

Commit 92c9940

Browse files
authored
Merge pull request #8 from shopwareLabs/fix-level-changes-on-move
fix level changes on move
2 parents 400f067 + f43423f commit 92c9940

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"Shopware\\DbalNestedSet\\": "src"
88
}
99
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"Shopware\\DbalNestedSetTest\\": "tests"
13+
}
14+
},
1015
"require": {
1116
"php": "^7.0",
1217
"doctrine/dbal": "^2.5"

src/NestedSetWriter.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ public function moveAsLastChild(string $tableExpression, string $rootColumnName,
207207
throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as last child of itself or into a descendant');
208208
}
209209

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

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

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

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

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

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

262-
$level = $sibling['level'] - $child['level'];
264+
$level = $sibling['level'];
263265

264-
$this->updateNodePosition($tableExpression, $child, $sibling['left'], $level);
266+
$this->updateLevel($tableExpression, $child['id'], $level);
267+
$this->updateNodePosition($tableExpression, $child, $sibling['left'], $level - $child['level']);
265268
}
266269

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

288-
$level = $sibling['level'] - $child['level'];
291+
$level = $sibling['level'];
289292

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

293297
/**
@@ -465,4 +469,18 @@ private function applyDeltaToSubtree(string $tableExpression, int $rootValue, in
465469
])
466470
->execute();
467471
}
472+
473+
/**
474+
* @param string $tableExpression
475+
* @param int $id
476+
* @param int $level
477+
*/
478+
private function updateLevel(string $tableExpression, int $id, int $level)
479+
{
480+
$this->connection->update(
481+
$tableExpression,
482+
[$this->levelCol => $level],
483+
[$this->pkCol => $id]
484+
);
485+
}
468486
}

tests/NestedSetWriterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,27 @@ public function test_move_as_last_child()
162162
$this->assertNode(11, 18, 19, 3, 1);
163163
}
164164

165+
public function test_move_as_last_child_with_different_levels()
166+
{
167+
\NestedSetBootstrap::insertDemoTree();
168+
169+
$this->writer->moveAsLastChild('tree', 'root_id', 3, 2); //make men last child of women
170+
171+
\NestedSetBootstrap::validateTree(1);
172+
173+
$this->assertNode(1, 1, 22, 0, 1);
174+
$this->assertNode(3, 2, 21, 1, 1);
175+
$this->assertNode(7, 3, 8, 2, 1);
176+
$this->assertNode(10, 4, 5, 3, 1);
177+
$this->assertNode(11, 6, 7, 3, 1);
178+
$this->assertNode(8, 9, 10, 2, 1);
179+
$this->assertNode(9, 11, 12, 2, 1);
180+
$this->assertNode(2, 13, 20, 2, 1);
181+
$this->assertNode(4, 14, 19, 3, 1);
182+
$this->assertNode(5, 15, 16, 4, 1);
183+
$this->assertNode(6, 17, 18, 4, 1);
184+
}
185+
165186
public function test_move_as_last_child_throws()
166187
{
167188
\NestedSetBootstrap::insertDemoTree();

tests/_bootstrap.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public static function insertDemoTree(int $rootId = 1)
9696
$data = [
9797
[1, 1, 22, 0, 'Clothing'],
9898
[2, 2, 9, 1, 'Mens'],
99-
[ 4, 3, 8, 2, 'Suits'],
100-
[ 5, 4, 5, 3, 'Slacks'],
101-
[ 6, 6, 7, 3, 'Jackets'],
102-
[ 3, 10, 21, 1, 'Women'],
103-
[ 7, 11, 16, 2, 'Dresses'],
99+
[4, 3, 8, 2, 'Suits'],
100+
[5, 4, 5, 3, 'Slacks'],
101+
[6, 6, 7, 3, 'Jackets'],
102+
[3, 10, 21, 1, 'Women'],
103+
[7, 11, 16, 2, 'Dresses'],
104104
[10, 12, 13, 3, 'Evening Growns'],
105105
[11, 14, 15, 3, 'Sun Dresses'],
106106
[8, 17, 18, 2, 'Skirts'],

0 commit comments

Comments
 (0)