2
2
3
3
namespace Spiral \NavigationBuilder \Services ;
4
4
5
+ use Spiral \Database \Builders \SelectQuery ;
5
6
use Spiral \Database \Injections \Parameter ;
6
7
use Spiral \NavigationBuilder \Database \Sources \LinkSource ;
7
8
use Spiral \NavigationBuilder \Database \Sources \TreeSource ;
@@ -68,16 +69,30 @@ public function saveStructure(string $domain, array $data)
68
69
$ this ->deleteDomainTree ($ domain );
69
70
70
71
//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 );
75
73
76
74
//Creates db tree records based on passed tree from builder UI
77
75
$ this ->createTree ($ domain , $ data , 1 );
78
76
$ this ->navigation ->rebuild ($ domain );
79
77
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
+ }
81
96
}
82
97
83
98
/**
@@ -87,7 +102,7 @@ public function saveStructure(string $domain, array $data)
87
102
*/
88
103
private function deleteDomainTree (string $ domain )
89
104
{
90
- $ this ->orm ->table (Tree::class)->delete ()->where (compact ('domain ' ));
105
+ $ this ->orm ->table (Tree::class)->delete ()->where (compact ('domain ' ))-> run () ;
91
106
}
92
107
93
108
/**
@@ -152,17 +167,13 @@ private function createTree(
152
167
153
168
/**
154
169
* Calculate link counters.
155
- *
156
- * @param array $keys
157
170
*/
158
- private function calculateCounters (array $ keys )
171
+ private function calculateCounters ()
159
172
{
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 ();
164
175
165
- $ links = $ query
176
+ $ links = $ request
166
177
->columns (['link.id ' , 'trees.domain ' , 'count(*) as count ' ])
167
178
->groupBy ('link.id, trees.domain ' );
168
179
@@ -188,4 +199,4 @@ private function calculateCounters(array $keys)
188
199
}
189
200
}
190
201
}
191
- }
202
+ }
0 commit comments