/** * 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 { // @todo: por aquĆ­ voy.... } $id = parent::insert($data); $this->_db->commit(); return $id; }