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 281<br />

Examining and Retrieving Siblings<br />

You can easily check if a node has any next or previous siblings by using the following<br />

methods:<br />

// test.php<br />

Listing<br />

18-12<br />

// ...<br />

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

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

You can also retrieve the next or previous siblings if they exist with the following methods:<br />

// test.php<br />

Listing<br />

18-13<br />

// ...<br />

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

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

The above methods return false if no next or previous sibling exists.<br />

If you want to retrieve an array of all the siblings you can simply use the getSiblings()<br />

method:<br />

// test.php<br />

Listing<br />

18-14<br />

// ...<br />

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

Examining and Retrieving Desc<strong>en</strong>dants<br />

You can check if a node has a par<strong>en</strong>t or childr<strong>en</strong> by using the following methods:<br />

// test.php<br />

Listing<br />

18-15<br />

// ...<br />

$hasChildr<strong>en</strong> = $category->getNode()->hasChildr<strong>en</strong>();<br />

$hasPar<strong>en</strong>t = $category->getNode()->hasPar<strong>en</strong>t();<br />

You can retrieve a nodes first and last child by using the following methods:<br />

// test.php<br />

Listing<br />

18-16<br />

// ...<br />

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

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

Or if you want to retrieve the par<strong>en</strong>t of a node:<br />

// test.php<br />

Listing<br />

18-17<br />

// ...<br />

$par<strong>en</strong>t = $category->getNode()->getPar<strong>en</strong>t();<br />

You can get the childr<strong>en</strong> of a node by using the following method:<br />

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

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

Saved successfully!

Ooh no, something went wrong!