Skip to content

Commit ac35e35

Browse files
author
Flaviu Chelaru
committed
fix1: consider the parent has changed IF the left key is not set. This is valueable for calculating edges for a node that has a parentID set, but the edges are not yet calculated
fix2: allow calculations of surrounding nodes only if the current node already has edges calculated
1 parent 4a0f480 commit ac35e35

File tree

1 file changed

+64
-43
lines changed

1 file changed

+64
-43
lines changed

src/Behavior.php

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -196,48 +196,16 @@ public function processUpdate(ModelInterface $model): void
196196
]);
197197

198198
// parent has changed (unset parent or set a new parent)
199-
if ($model->readAttribute(self::$parentKey) == $currentModel->readAttribute(self::$parentKey)) {
199+
// or current left is not set
200+
if (
201+
$model->readAttribute(self::$leftKey) > 0
202+
&& $model->readAttribute(self::$parentKey) == $currentModel->readAttribute(self::$parentKey)
203+
) {
200204
return;
201205
}
202206

203-
// upgrade children of current node in the tree
204-
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
205-
'`' . self::$depthDbColumn . '` = `' . self::$depthDbColumn . '` - 1, ' .
206-
'`' . self::$rightDbColumn . '` = `' . self::$rightDbColumn . '` - 1, ' .
207-
'`' . self::$leftDbColumn . '` = `' . self::$leftDbColumn . '` - 1 ' .
208-
'WHERE `' . self::$leftDbColumn . '` > :left AND `' . self::$rightDbColumn . '` < :right ' .
209-
'AND `' . self::$primaryDbColumn . '` != :id';
210-
$model->getWriteConnection()->query($query, [
211-
'right' => $currentModel->readAttribute(self::$rightKey),
212-
'left' => $currentModel->readAttribute(self::$leftKey),
213-
'id' => $model->readAttribute(self::$primaryKey),
214-
]);
215-
216-
// update parent for immediate sub-nodes of current category
217-
$query = 'UPDATE `' . $model->getSource() . '` SET' .
218-
' `' . self::$parentDbColumn . '` = :parent ' .
219-
'WHERE `' . self::$parentDbColumn . '` = :id';
220-
$model->getWriteConnection()->query($query, [
221-
'id' => $currentModel->readAttribute(self::$primaryKey),
222-
'parent' => $currentModel->readAttribute(self::$parentKey)
223-
]);
224-
225-
// update nodes on the right of the tree
226-
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
227-
'`' . self::$rightDbColumn . '` = `' . self::$rightDbColumn . '` - 2 ' .
228-
'WHERE `' . self::$rightDbColumn . '` > :right AND `' . self::$primaryDbColumn . '` != :id';
229-
$model->getWriteConnection()->query($query, [
230-
'right' => $currentModel->readAttribute(self::$rightKey),
231-
'id' => $model->readAttribute(self::$primaryKey),
232-
]);
233-
234-
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
235-
'`' . self::$leftDbColumn . '` = `' . self::$leftDbColumn . '` - 2 ' .
236-
'WHERE `' . self::$leftDbColumn . '` > :right AND `' . self::$primaryDbColumn . '` != :id';
237-
$model->getWriteConnection()->query($query, [
238-
'right' => $currentModel->readAttribute(self::$rightKey),
239-
'id' => $model->readAttribute(self::$primaryKey),
240-
]);
207+
// upgrade children of current node in the tree, but only if we currently have a tree set
208+
$this->preUpdateHook($model, $currentModel);
241209

242210
$maxRight = $model::maximum(['column' => self::$rightKey]);
243211
// unset the parent - make the node a root
@@ -254,12 +222,16 @@ public function processUpdate(ModelInterface $model): void
254222

255223
$right = $parentModel->readAttribute(self::$rightKey);
256224
$depth = $parentModel->readAttribute(self::$depthKey);
257-
if ($parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$rightKey)
258-
&& $parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$leftKey)) {
225+
if (
226+
$parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$rightKey)
227+
&& $parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$leftKey)
228+
) {
259229
$right = $parentModel->readAttribute(self::$rightKey) - 2;
260230
$depth = $parentModel->readAttribute(self::$depthKey) + 1;
261-
} elseif ($parentModel->readAttribute(self::$rightKey) < $currentModel->readAttribute(self::$rightKey)
262-
&& $parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$leftKey)) {
231+
} elseif (
232+
$parentModel->readAttribute(self::$rightKey) < $currentModel->readAttribute(self::$rightKey)
233+
&& $parentModel->readAttribute(self::$rightKey) > $currentModel->readAttribute(self::$leftKey)
234+
) {
263235
$right = $parentModel->readAttribute(self::$rightKey) - 1;
264236
$depth = $parentModel->readAttribute(self::$depthKey);
265237
}
@@ -285,4 +257,53 @@ public function processUpdate(ModelInterface $model): void
285257
self::$parentKey => $parentModel->readAttribute(self::$primaryKey),
286258
]);
287259
}
260+
261+
/**
262+
* @param ModelInterface $model
263+
* @param $currentModel
264+
*/
265+
private function preUpdateHook(ModelInterface $model, ModelInterface $currentModel): void
266+
{
267+
if (!$currentModel->readAttribute(self::$rightKey) > 0) {
268+
return;
269+
}
270+
271+
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
272+
'`' . self::$depthDbColumn . '` = `' . self::$depthDbColumn . '` - 1, ' .
273+
'`' . self::$rightDbColumn . '` = `' . self::$rightDbColumn . '` - 1, ' .
274+
'`' . self::$leftDbColumn . '` = `' . self::$leftDbColumn . '` - 1 ' .
275+
'WHERE `' . self::$leftDbColumn . '` > :left AND `' . self::$rightDbColumn . '` < :right ' .
276+
'AND `' . self::$primaryDbColumn . '` != :id';
277+
$model->getWriteConnection()->query($query, [
278+
'right' => $currentModel->readAttribute(self::$rightKey),
279+
'left' => $currentModel->readAttribute(self::$leftKey),
280+
'id' => $model->readAttribute(self::$primaryKey),
281+
]);
282+
283+
// update parent for immediate sub-nodes of current category
284+
$query = 'UPDATE `' . $model->getSource() . '` SET' .
285+
' `' . self::$parentDbColumn . '` = :parent ' .
286+
'WHERE `' . self::$parentDbColumn . '` = :id';
287+
$model->getWriteConnection()->query($query, [
288+
'id' => $currentModel->readAttribute(self::$primaryKey),
289+
'parent' => $currentModel->readAttribute(self::$parentKey)
290+
]);
291+
292+
// update nodes on the right of the tree
293+
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
294+
'`' . self::$rightDbColumn . '` = `' . self::$rightDbColumn . '` - 2 ' .
295+
'WHERE `' . self::$rightDbColumn . '` > :right AND `' . self::$primaryDbColumn . '` != :id';
296+
$model->getWriteConnection()->query($query, [
297+
'right' => $currentModel->readAttribute(self::$rightKey),
298+
'id' => $model->readAttribute(self::$primaryKey),
299+
]);
300+
301+
$query = 'UPDATE `' . $model->getSource() . '` SET ' .
302+
'`' . self::$leftDbColumn . '` = `' . self::$leftDbColumn . '` - 2 ' .
303+
'WHERE `' . self::$leftDbColumn . '` > :right AND `' . self::$primaryDbColumn . '` != :id';
304+
$model->getWriteConnection()->query($query, [
305+
'right' => $currentModel->readAttribute(self::$rightKey),
306+
'id' => $model->readAttribute(self::$primaryKey),
307+
]);
308+
}
288309
}

0 commit comments

Comments
 (0)