Skip to content

Commit 6e779ca

Browse files
authored
feat: optimizations to menu persistance (#219)
Co-authored-by: Florian ALEXANDRE <f.alexandre@novactive.com>
2 parents e38f578 + 2953e1c commit 6e779ca

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

components/MenuManagerBundle/src/bundle/Controller/AdminController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public function editAction(Request $request, Menu $menu)
162162
if ($form->isSubmitted() && $form->isValid()) {
163163
/** @var Menu $menu */
164164
$menu = $form->getData();
165+
$menu->assignPositions();
165166
$this->em->persist($menu);
166167
$this->em->flush();
167168

components/MenuManagerBundle/src/bundle/Entity/Menu.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,15 @@ public function __toString()
178178
{
179179
return (string) $this->id;
180180
}
181+
182+
public function assignPositions(): void
183+
{
184+
$rootItems = $this->getItemsByParent();
185+
$position = 0;
186+
foreach ($rootItems as $item) {
187+
$item->setPosition($position);
188+
$item->assignPositions();
189+
++$position;
190+
}
191+
}
181192
}

components/MenuManagerBundle/src/bundle/Entity/MenuItem.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Novactive\EzMenuManagerBundle\Entity;
1414

1515
use Doctrine\Common\Collections\ArrayCollection;
16+
use Doctrine\Common\Collections\Criteria;
1617
use Doctrine\ORM\Mapping as ORM;
1718
use Novactive\EzMenuManager\Traits\IdentityTrait;
1819

@@ -203,7 +204,10 @@ public function hasChildrens(): bool
203204
*/
204205
public function getChildrens()
205206
{
206-
return $this->childrens;
207+
$criteria = new Criteria();
208+
$criteria->orderBy(['position' => Criteria::ASC]);
209+
210+
return $this->childrens->matching($criteria);
207211
}
208212

209213
/**
@@ -303,7 +307,20 @@ public function __toString()
303307
public function update(array $properties): void
304308
{
305309
foreach ($properties as $property => $value) {
306-
$this->$property = $value;
310+
if ($this->$property !== $value) {
311+
$this->$property = $value;
312+
}
313+
}
314+
}
315+
316+
public function assignPositions(): void
317+
{
318+
$childrens = $this->getChildrens();
319+
$position = 0;
320+
foreach ($childrens as $child) {
321+
$child->setPosition($position);
322+
$child->assignPositions();
323+
++$position;
307324
}
308325
}
309326
}

components/MenuManagerBundle/src/lib/Form/Type/FieldType/FieldValueTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function reverseTransform($value)
7878
}
7979
$parent = $menuItemRepo->find($hashItem['parentId']);
8080
if ($hashItem['parentId'] && $parent) {
81-
$menuItem->setParent($parent);
81+
$parent->addChildren($menuItem);
8282
}
8383
$menuItem->setMenu($menuRepo->find($hashItem['menuId']));
8484
$menuItem->setPosition($hashItem['position'] ?? 0);

components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function fromHash($hash): ?MenuItem
7676
'name' => $hash['name'] ?? false,
7777
'url' => $hash['url'] ?? false,
7878
'target' => $hash['target'] ?? false,
79-
'position' => $hash['position'] ?? 0,
79+
'position' => (int) $hash['position'] ?? 0,
8080
];
8181
$menuItem->update(array_filter($updateData));
8282

@@ -85,11 +85,12 @@ public function fromHash($hash): ?MenuItem
8585
$menuItem->setOption($option, $value);
8686
}
8787

88-
if (isset($hash['parentId']) && $hash['parentId']) {
88+
$currentParent = $menuItem->getParent();
89+
if ($currentParent && (!isset($hash['parentId']) || null === $hash['parentId'])) {
90+
$currentParent->removeChildren($menuItem);
91+
} elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== $hash['parentId'])) {
8992
$parent = $menuItemRepo->find($hash['parentId']);
90-
$menuItem->setParent($parent);
91-
} else {
92-
$menuItem->setParent(null);
93+
$parent->addChildren($menuItem);
9394
}
9495

9596
if (isset($hash['menuId'])) {

0 commit comments

Comments
 (0)