public function insertAsRoot()
{
// You can't insert a node that is already saved i.e. the table has an id
if ($this->getId()) {
throw new TreeMethodOnlyAllowedInRoot(__METHOD__);
}
// First we need to find the right value of the last parent, a.k.a. the max(rgt) of the table
$db = $this->getDbo();
// Get the lft/rgt names
$fldRgt = $db->qn($this->getFieldAlias('rgt'));
$query = $db->getQuery(true)->select('MAX(' . $fldRgt . ')')->from($db->qn($this->tableName));
$maxRgt = $db->setQuery($query, 0, 1)->loadResult();
if (empty($maxRgt)) {
$maxRgt = 0;
}
$this->lft = ++$maxRgt;
$this->rgt = ++$maxRgt;
return $this->save();
}