All pastes #924780 Raw Edit

StR

public php v1 · immutable
·published 2008-03-01 18:26 UTC
rendered paste body
    /**     * Inserts a new leaf, it is inserted at the end, as an append     *     * @param array             $data   Data to insert     * @param Biznet_Model_Tree $parent Parent to this leaf     *     * @return int     */    public function insert($data, Biznet_Model_Tree $parent=NULL)    {        $this->_db->beginTransaction();        if ($parent) {            $sql = "UPDATE {$this->_name} set rgt=rgt+2 where rgt >= ?";            $this->_db->query($sql, $parent->getRight());            $sql = "UPDATE {$this->_name} set lft=lft+2 where lft > ?";            $this->_db->query($sql, $parent->getRight());            $brothers = $parent->getChildren();            // Checks if parent is empty            if (!count($brothers)) {                $data['lft'] = $parent->getLeft() +1;                $data['rgt'] = $parent->getLeft() +2;            } else {                $lastBroter  = $brothers[count($brothers)];                $data['lft'] = $lastBrother->getRight() +1;                $data['rgt'] = $lastBrother->getRight() +2;            }        } else {            // Checks if I'm the first            $parentLevel = $this->getBase(0);            if (!count($parentLevel)) {                $data['lft'] = 1;                $data['rgt'] = 2;            } else {                $lastBaseLevel = $parentLevel[count($parentLevel)];                $data['lft'] = $lastBaseLevel->getRight() +1;                $data['rgt'] = $lastBaseLevel->getRight() +2;            }        }        $id =  parent::insert($data);        $this->_db->commit();        return $id;    }