public function rebuildPath($pk = null)
    {
        $fields = $this->getFields();

        // If there is no alias or path field, just return true.
        if (!array_key_exists('alias', $fields) || !array_key_exists('path', $fields)) {
            return $this;
        }

        $k = $this->getKeyName();
        $pk = $pk === null ? $this->$k : $pk;

        // Get the aliases for the path from the node to the root node.
        $query = $this->db->getQuery(true)
            ->select('p.alias')
            ->from($this->db->quoteName($this->table) . ' AS n, ' . $this->db->quoteName($this->table) . ' AS p')
            ->where('n.lft BETWEEN p.lft AND p.rgt')
            ->where('n.' . $this->db->quoteName($this->getKeyName()) . ' = ' . (int) $pk)
            ->order('p.lft');

        $this->db->setQuery($query);

        $segments = $this->db->loadColumn();

        // Make sure to remove the root path if it exists in the list.
        if ($segments[0] === 'root') {
            array_shift($segments);
        }

        // Build the path.
        $path = trim(implode('/', $segments), ' /\\');

        // Update the path field for the node.
        $query->clear()
            ->update($this->db->quoteName($this->table))
            ->set('path = ' . $this->db->quote($path))
            ->where($this->getKeyName() . ' = ' . (int) $pk);

        $this->db->setQuery($query)->execute();

        // Update the current record's path to the new one:
        $this->path = $path;

        return $this;
    }