FOF30\Model\TreeModel::insertAsRoot PHP Method

insertAsRoot() public method

Insert the current node as a tree root. It is a good idea to never use this method, instead providing a root node in your schema installation and then sticking to only one root.
public insertAsRoot ( ) : static
return static
    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();
    }