10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 18: Hierarchical Data 280<br />

// ...<br />

$category = <strong>Doctrine</strong>_Core::getTable('Category')->findOneByName('Child<br />

Category 1');<br />

$category->getNode()->delete();<br />

The above code calls $category->delete() internally. It's important to delete on the<br />

node and not on the record. Otherwise you may corrupt the tree.<br />

Deleting a node will also delete all desc<strong>en</strong>dants of that node. So make sure you move them<br />

elsewhere before you delete the node if you don't want to delete them.<br />

Moving a Node<br />

Moving a node is simple. <strong>Doctrine</strong> offers several methods for moving nodes around betwe<strong>en</strong><br />

trees:<br />

Listing<br />

18-10<br />

// test.php<br />

// ...<br />

$category = new Category();<br />

$category->name = 'Root Category 2';<br />

$category->save();<br />

$categoryTable = <strong>Doctrine</strong>_Core::getTable('Category');<br />

$treeObject = $categoryTable->getTree();<br />

$treeObject->createRoot($category);<br />

$childCategory = $categoryTable->findOneByName('Child Category 1');<br />

$childCategory->getNode()->moveAsLastChildOf($category);<br />

...<br />

Below is a list of the methods available for moving nodes around:<br />

• moveAsLastChildOf($other)<br />

• moveAsFirstChildOf($other)<br />

• moveAsPrevSiblingOf($other)<br />

• moveAsNextSiblingOf($other).<br />

The method names should be self-explanatory to you.<br />

Examining a Node<br />

You can examine the nodes and what type of node they are by using some of the following<br />

functions:<br />

Listing<br />

18-11<br />

// test.php<br />

// ...<br />

$isLeaf = $category->getNode()->isLeaf();<br />

$isRoot = $category->getNode()->isRoot();<br />

The above used functions return true/false dep<strong>en</strong>ding on whether or not they are a leaf or<br />

root node.<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!