Chapter 18: Hierarchical Data 280 // ... $category = Doctrine_Core::getTable('Category')->findOneByName('Child Category 1'); $category->getNode()->delete(); The above code calls $category->delete() internally. It's important to delete on the node and not on the record. Otherwise you may corrupt the tree. Deleting a node will also delete all descendants of that node. So make sure you move them elsewhere before you delete the node if you don't want to delete them. Moving a Node Moving a node is simple. Doctrine offers several methods for moving nodes around between trees: Listing 18-10 // test.php // ... $category = new Category(); $category->name = 'Root Category 2'; $category->save(); $categoryTable = Doctrine_Core::getTable('Category'); $treeObject = $categoryTable->getTree(); $treeObject->createRoot($category); $childCategory = $categoryTable->findOneByName('Child Category 1'); $childCategory->getNode()->moveAsLastChildOf($category); ... Below is a list of the methods available for moving nodes around: • moveAsLastChildOf($other) • moveAsFirstChildOf($other) • moveAsPrevSiblingOf($other) • moveAsNextSiblingOf($other). The method names should be self-explanatory to you. Examining a Node You can examine the nodes and what type of node they are by using some of the following functions: Listing 18-11 // test.php // ... $isLeaf = $category->getNode()->isLeaf(); $isRoot = $category->getNode()->isRoot(); The above used functions return true/false depending on whether or not they are a leaf or root node. ----------------- Brought to you by

Chapter 18: Hierarchical Data 281 Examining and Retrieving Siblings You can easily check if a node has any next or previous siblings by using the following methods: // test.php Listing 18-12 // ... $hasNextSib = $category->getNode()->hasNextSibling(); $hasPrevSib = $category->getNode()->hasPrevSibling(); You can also retrieve the next or previous siblings if they exist with the following methods: // test.php Listing 18-13 // ... $nextSib = $category->getNode()->getNextSibling(); $prevSib = $category->getNode()->getPrevSibling(); The above methods return false if no next or previous sibling exists. If you want to retrieve an array of all the siblings you can simply use the getSiblings() method: // test.php Listing 18-14 // ... $siblings = $category->getNode()->getSiblings(); Examining and Retrieving Descendants You can check if a node has a parent or children by using the following methods: // test.php Listing 18-15 // ... $hasChildren = $category->getNode()->hasChildren(); $hasParent = $category->getNode()->hasParent(); You can retrieve a nodes first and last child by using the following methods: // test.php Listing 18-16 // ... $firstChild = $category->getNode()->getFirstChild(); $lastChild = $category->getNode()->getLastChild(); Or if you want to retrieve the parent of a node: // test.php Listing 18-17 // ... $parent = $category->getNode()->getParent(); You can get the children of a node by using the following method: ----------------- Brought to you by