Skip to content

Commit 3d28a6d

Browse files
committed
Add hasMoved() and save in transaction
1 parent d93f571 commit 3d28a6d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/Kalnoy/Nestedset/Node.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class Node extends Eloquent {
6161
*/
6262
protected $pending = [ 'root' ];
6363

64+
/**
65+
* Whether the node has moved since last save.
66+
*
67+
* @var bool
68+
*/
69+
protected $moved = false;
70+
6471
/**
6572
* Keep track of the number of performed operations.
6673
*
@@ -117,6 +124,20 @@ protected static function signOnEvents()
117124
}
118125
}
119126

127+
/**
128+
* {@inheritdoc}
129+
*
130+
* Saves a node in a transaction.
131+
*
132+
*/
133+
public function save(array $options = array())
134+
{
135+
return $this->getConnection()->transaction(function () use ($options)
136+
{
137+
return parent::save($options);
138+
});
139+
}
140+
120141
/**
121142
* Set an action.
122143
*
@@ -144,14 +165,16 @@ protected function clearAction()
144165
*/
145166
protected function callPendingAction()
146167
{
168+
$this->moved = false;
169+
147170
if ( ! $this->pending) return;
148171

149172
$method = 'action'.ucfirst(array_shift($this->pending));
150173
$parameters = $this->pending;
151174

152175
$this->pending = null;
153176

154-
return call_user_func_array([ $this, $method ], $parameters);
177+
$this->moved = call_user_func_array([ $this, $method ], $parameters);
155178
}
156179

157180
/**
@@ -170,6 +193,8 @@ protected function actionRoot()
170193
return true;
171194
}
172195

196+
if ($this->isRoot()) return false;
197+
173198
// Reset parent object
174199
$this->setParent(null);
175200

@@ -290,7 +315,6 @@ public function refreshNode()
290315
$attributes = $this->newServiceQuery()->getNodeData($this->getKey());
291316

292317
$this->attributes = array_merge($this->attributes, $attributes);
293-
$this->original = array_merge($this->original, $attributes);
294318
}
295319

296320
/**
@@ -595,11 +619,11 @@ protected function insertAt($position)
595619
*/
596620
protected function moveNode($position)
597621
{
598-
$updated = $this->newServiceQuery()->moveNode($this->getKey(), $position);
622+
$updated = $this->newServiceQuery()->moveNode($this->getKey(), $position) > 0;
599623

600624
if ($updated) $this->refreshNode();
601625

602-
return $updated > 0;
626+
return $updated;
603627
}
604628

605629
/**
@@ -941,4 +965,14 @@ public function isBroken()
941965
{
942966
return $this->getTotalErrors() > 0;
943967
}
968+
969+
/**
970+
* Get whether the node has moved since last save.
971+
*
972+
* @return bool
973+
*/
974+
public function hasMoved()
975+
{
976+
return $this->moved;
977+
}
944978
}

src/Kalnoy/Nestedset/QueryBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ public function moveNode($key, $position)
375375
// The distance that our node will travel to reach it's destination
376376
$distance = $to - $from + 1 - $height;
377377

378+
// If no distance to travel, just return
379+
if ($distance === 0) return 0;
380+
378381
if ($position > $lft) $height *= -1; else $distance *= -1;
379382

380383
$params = compact('lft', 'rgt', 'from', 'to', 'height', 'distance');

0 commit comments

Comments
 (0)