/** * Returns if the element is root. * * @param $model|App_NestedSet_Model Nested set model * @param $elementId|int Element ID * * @return boolean */ public function isRoot(App_NestedSet_Model $nestedset, $elementId) { $db = $nestedset->getDb(); $query = "\n SELECT 1\n FROM {$nestedset->getTableName()}\n WHERE {$nestedset->getStructureId()} = {$elementId}\n AND {$nestedset->getStructureLeft()} = (\n SELECT MIN({$nestedset->getStructureLeft()})\n FROM {$nestedset->getTableName()}\n )\n AND {$nestedset->getStructureRight()} = (\n SELECT MAX({$nestedset->getStructureRight()})\n FROM {$nestedset->getTableName()}\n )\n "; $stmt = $db->query($query); $result = $stmt->fetchColumn(); return (bool) $result; }
/** * Delete a node, but move all its children outside first * * @param $model|App_NestedSet_Model Nested set model * @param $tree|array * * @return $this */ public function deleteNonRecursive(App_NestedSet_Model $nestedset, array $tree) { $db = $nestedset->getDb(); $left = (int) $tree[$nestedset->getStructureLeft()]; $right = (int) $tree[$nestedset->getStructureRight()]; try { $db->beginTransaction(); $delete = $db->delete($nestedset->getTableName(), "{$nestedset->getStructureId()} = {$tree[$nestedset->getStructureId()]}"); // update other elements $width = 2; // update right for inner elements $stmt = $db->query("\n UPDATE {$nestedset->getTableName()}\n SET {$nestedset->getStructureRight()} = {$nestedset->getStructureRight()} - 1\n WHERE {$nestedset->getStructureLeft()} > {$left} AND {$nestedset->getStructureRight()} < {$right}\n "); $update = $stmt->fetch(); // update left for inner elements $stmt = $db->query("\n UPDATE {$nestedset->getTableName()}\n SET {$nestedset->getStructureLeft()} = {$nestedset->getStructureLeft()} - 1\n WHERE {$nestedset->getStructureLeft()} > {$left} AND {$nestedset->getStructureRight()} < {$right}\n "); $update = $stmt->fetch(); // update right for outer elements $stmt = $db->query("\n UPDATE {$nestedset->getTableName()}\n SET {$nestedset->getStructureRight()} = {$nestedset->getStructureRight()} - {$width}\n WHERE {$nestedset->getStructureRight()} > {$right}\n "); $update = $stmt->fetch(); // update left for outer elements $stmt = $db->query("\n UPDATE {$nestedset->getTableName()}\n SET {$nestedset->getStructureLeft()} = {$nestedset->getStructureLeft()} - {$width}\n WHERE {$nestedset->getStructureLeft()} > {$left} AND {$nestedset->getStructureRight()} >= {$right}\n "); $update = $stmt->fetch(); $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } return $this; }