From 301e3e2274d4d9db8774e4aa468f93b755b5decf Mon Sep 17 00:00:00 2001 From: Christoph Weygand Date: Thu, 11 Jan 2024 11:38:46 +0100 Subject: [PATCH 1/3] Allow multiple order critieria in NestedTreeRepository::reorder and NestedTreeRepository:reorderAll --- .../Repository/NestedTreeRepository.php | 16 ++++++------ .../Tree/NestedTreeRootRepositoryTest.php | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Tree/Entity/Repository/NestedTreeRepository.php b/src/Tree/Entity/Repository/NestedTreeRepository.php index fe5bdf8337..d4aa346fec 100644 --- a/src/Tree/Entity/Repository/NestedTreeRepository.php +++ b/src/Tree/Entity/Repository/NestedTreeRepository.php @@ -801,11 +801,11 @@ public function removeFromTree($node) * Reorders $node's child nodes, * according to the $sortByField and $direction specified * - * @param object|null $node node from which to start reordering the tree; null will reorder everything - * @param string $sortByField field name to sort by - * @param string $direction sort direction : "ASC" or "DESC" - * @param bool $verify true to verify tree first - * @param bool $recursive true to also reorder further descendants, not just the direct children + * @param object|null $node node from which to start reordering the tree; null will reorder everything + * @param string|string[]|null $sortByField Field name or array of fields names to sort by + * @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 + * @param bool $verify true to verify tree first + * @param bool $recursive true to also reorder further descendants, not just the direct children * * @return void */ @@ -836,9 +836,9 @@ public function reorder($node, $sortByField = null, $direction = 'ASC', $verify /** * Reorders all nodes in the tree according to the $sortByField and $direction specified. * - * @param string $sortByField field name to sort by - * @param string $direction sort direction : "ASC" or "DESC" - * @param bool $verify true to verify tree first + * @param string|string[]|null $sortByField Field name or array of fields names to sort by + * @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 + * @param bool $verify true to verify tree first * * @return void */ diff --git a/tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php b/tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php index 5b624fe755..45aaba950d 100644 --- a/tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php +++ b/tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php @@ -418,6 +418,31 @@ public function testShouldHandleAdvancedRepositoryFunctions(): void static::assertSame(9, $node->getLeft()); static::assertSame(10, $node->getRight()); + // reorder (multiple order critieria) + + $node = $repo->findOneBy(['title' => 'Vegitables']); + $repo->reorder($node, ['level', 'title'], ['ASC', 'DESC'], false, false); + + $node = $repo->findOneBy(['title' => 'Cabbages']); + + static::assertSame(9, $node->getLeft()); + static::assertSame(10, $node->getRight()); + + $node = $repo->findOneBy(['title' => 'Carrots']); + + static::assertSame(7, $node->getLeft()); + static::assertSame(8, $node->getRight()); + + $node = $repo->findOneBy(['title' => 'Onions']); + + static::assertSame(5, $node->getLeft()); + static::assertSame(6, $node->getRight()); + + $node = $repo->findOneBy(['title' => 'Potatoes']); + + static::assertSame(3, $node->getLeft()); + static::assertSame(4, $node->getRight()); + // reorder $node = $repo->findOneBy(['title' => 'Food']); From 15a106e4bbd428493cd3fffe3903620c33225fd3 Mon Sep 17 00:00:00 2001 From: Christoph Weygand Date: Thu, 11 Jan 2024 11:46:26 +0100 Subject: [PATCH 2/3] Added changelog entry for reorder/reorderAll modification --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0668f8c14..383b1e6c5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,9 @@ a release. - Make doctrine/annotations an optional dependency. - Remove `@internal` annotation from `Gedmo\Mapping\Driver\AttributeReader`. +## Changed +- Allow string[] as `$sortByField` and `$direction` parameter in `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorder` and `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorderAll` + ### Deprecated - Do not add type-hinted parameters `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes to `preSoftDelete` and `postSoftDelete` events. From 45f5af4c4602bec98d98accde3957a3198ca072b Mon Sep 17 00:00:00 2001 From: Christoph Weygand Date: Sat, 18 Jan 2025 10:37:45 +0100 Subject: [PATCH 3/3] Updated changelog according to review suggestions --- CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 383b1e6c5e..5508d1afa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,10 @@ a release. ### Deprecated - Sluggable: Annotation-specific mapping parameters (#2837) +### Changed +- Allow `string[]` as `$sortByField` and `$direction` parameter in `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorder()` and `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorderAll()` + + ### Fixed - Fix regression with `doctrine/dbal` >= 4.0 that caused MariaDB to improperly attempt LONGTEXT casting in `TranslationWalker` (issue #2887) - Tree: allow usage of UuidV4 as path source with the materialized path strategy @@ -67,9 +71,6 @@ a release. - Make doctrine/annotations an optional dependency. - Remove `@internal` annotation from `Gedmo\Mapping\Driver\AttributeReader`. -## Changed -- Allow string[] as `$sortByField` and `$direction` parameter in `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorder` and `\Gedmo\Tree\Entity\Repository\NestedTreeRepository::reorderAll` - ### Deprecated - Do not add type-hinted parameters `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes to `preSoftDelete` and `postSoftDelete` events.