/**
* Recursive method used to recover a single level of the tree
*
* @param int $counter The Last left column value that was assigned
* @param mixed $parentId the parent id of the level to be recovered
* @return int Ne next value to use for the left column
*/
protected function _recoverTree($counter = 0, $parentId = null)
{
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$pk = (array) $this->_table->primaryKey();
$query = $this->_scope($this->_table->query())->select($pk)->where(function ($exp) use($parentId, $parent) {
return $parentId === null ? $exp->isNull($parent) : $exp->eq($parent, $parentId);
})->order($pk)->hydrate(false)->bufferResults(false);
$leftCounter = $counter;
foreach ($query as $row) {
$counter++;
$counter = $this->_recoverTree($counter, $row[$pk[0]]);
}
if ($parentId === null) {
return $counter;
}
$this->_table->updateAll([$left => $leftCounter, $right => $counter + 1], [$pk[0] => $parentId]);
return $counter + 1;
}