Skip to content

Commit 040051c

Browse files
author
Christoph Weygand
committed
Allow multiple order critieria in NestedTreeRepository::reorder and NestedTreeRepository:reorderAll
1 parent e3da653 commit 040051c

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/Tree/Entity/Repository/NestedTreeRepository.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -886,11 +886,11 @@ public function removeFromTree($node)
886886
* Reorders $node's child nodes,
887887
* according to the $sortByField and $direction specified
888888
*
889-
* @param object|null $node node from which to start reordering the tree; null will reorder everything
890-
* @param string $sortByField field name to sort by
891-
* @param string $direction sort direction : "ASC" or "DESC"
892-
* @param bool $verify true to verify tree first
893-
* @param bool $recursive true to also reorder further descendants, not just the direct children
889+
* @param object|null $node node from which to start reordering the tree; null will reorder everything
890+
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
891+
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
892+
* @param bool $verify true to verify tree first
893+
* @param bool $recursive true to also reorder further descendants, not just the direct children
894894
*
895895
* @return void
896896
*/
@@ -921,9 +921,9 @@ public function reorder($node, $sortByField = null, $direction = 'ASC', $verify
921921
/**
922922
* Reorders all nodes in the tree according to the $sortByField and $direction specified.
923923
*
924-
* @param string $sortByField field name to sort by
925-
* @param string $direction sort direction : "ASC" or "DESC"
926-
* @param bool $verify true to verify tree first
924+
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
925+
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
926+
* @param bool $verify true to verify tree first
927927
*
928928
* @return void
929929
*/

tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,31 @@ public function testShouldHandleAdvancedRepositoryFunctions(): void
420420
static::assertSame(9, $node->getLeft());
421421
static::assertSame(10, $node->getRight());
422422

423+
// reorder (multiple order critieria)
424+
425+
$node = $repo->findOneBy(['title' => 'Vegitables']);
426+
$repo->reorder($node, ['level', 'title'], ['ASC', 'DESC'], false, false);
427+
428+
$node = $repo->findOneBy(['title' => 'Cabbages']);
429+
430+
static::assertSame(9, $node->getLeft());
431+
static::assertSame(10, $node->getRight());
432+
433+
$node = $repo->findOneBy(['title' => 'Carrots']);
434+
435+
static::assertSame(7, $node->getLeft());
436+
static::assertSame(8, $node->getRight());
437+
438+
$node = $repo->findOneBy(['title' => 'Onions']);
439+
440+
static::assertSame(5, $node->getLeft());
441+
static::assertSame(6, $node->getRight());
442+
443+
$node = $repo->findOneBy(['title' => 'Potatoes']);
444+
445+
static::assertSame(3, $node->getLeft());
446+
static::assertSame(4, $node->getRight());
447+
423448
// reorder
424449

425450
$node = $repo->findOneBy(['title' => 'Food']);

0 commit comments

Comments
 (0)