Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 1aa961c

Browse files
committed
M delete tree bugfix
1 parent fa1301b commit 1aa961c

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

source/NavigationBuilder/Controllers/NavigationController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public function saveAction(
291291

292292
return [
293293
'status' => 200,
294-
'links' => $navigation->getTree($id)
294+
'links' => $navigation->getTree($id, false)
295295
];
296296
}
297297
}

source/NavigationBuilder/Services/Builder.php

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spiral\NavigationBuilder\Services;
44

5+
use Spiral\Database\Builders\SelectQuery;
56
use Spiral\Database\Injections\Parameter;
67
use Spiral\NavigationBuilder\Database\Sources\LinkSource;
78
use Spiral\NavigationBuilder\Database\Sources\TreeSource;
@@ -68,16 +69,30 @@ public function saveStructure(string $domain, array $data)
6869
$this->deleteDomainTree($domain);
6970

7071
//load all links from db
71-
$links = new KeysExtractor($data);
72-
$this->stack->setLinks($this->linkSource->find([
73-
'id' => ['IN' => new Parameter($links->getKeys())]
74-
]));
72+
$this->loadStack($data);
7573

7674
//Creates db tree records based on passed tree from builder UI
7775
$this->createTree($domain, $data, 1);
7876
$this->navigation->rebuild($domain);
7977

80-
$this->calculateCounters($links->getKeys());
78+
$this->calculateCounters();
79+
}
80+
81+
/**
82+
* Load passed links to stack instance. No keys (empty tree) - nothing to load
83+
*
84+
* @param array $data
85+
*/
86+
private function loadStack(array $data)
87+
{
88+
$links = new KeysExtractor($data);
89+
$keys = $links->getKeys();
90+
91+
if (count($keys)) {
92+
$this->stack->setLinks($this->linkSource->find([
93+
'id' => ['IN' => new Parameter($keys)]
94+
]));
95+
}
8196
}
8297

8398
/**
@@ -87,7 +102,7 @@ public function saveStructure(string $domain, array $data)
87102
*/
88103
private function deleteDomainTree(string $domain)
89104
{
90-
$this->orm->table(Tree::class)->delete()->where(compact('domain'));
105+
$this->orm->table(Tree::class)->delete()->where(compact('domain'))->run();
91106
}
92107

93108
/**
@@ -152,17 +167,13 @@ private function createTree(
152167

153168
/**
154169
* Calculate link counters.
155-
*
156-
* @param array $keys
157170
*/
158-
private function calculateCounters(array $keys)
171+
private function calculateCounters()
159172
{
160-
/** @var SelectQuery $query */
161-
$query = $this->linkSource->findWithTree()->where([
162-
'link.id' => ['IN' => new Parameter($keys)]
163-
])->compiledQuery();
173+
/** @var SelectQuery $request */
174+
$request = $this->linkSource->findWithTree()->compiledQuery();
164175

165-
$links = $query
176+
$links = $request
166177
->columns(['link.id', 'trees.domain', 'count(*) as count'])
167178
->groupBy('link.id, trees.domain');
168179

@@ -188,4 +199,4 @@ private function calculateCounters(array $keys)
188199
}
189200
}
190201
}
191-
}
202+
}

0 commit comments

Comments
 (0)