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 16: Behaviors 255<br />

$ php test.php<br />

Array<br />

(<br />

[id] => 1<br />

[Translation] => Array<br />

(<br />

[<strong>en</strong>] => Array<br />

(<br />

[id] => 1<br />

[title] => some title<br />

[body] => test<br />

[lang] => <strong>en</strong><br />

)<br />

[fi] => Array<br />

(<br />

[id] => 1<br />

[title] => joku otsikko<br />

[body] => test<br />

[lang] => fi<br />

)<br />

Listing<br />

16-46<br />

)<br />

)<br />

How do we retrieve the translated data now? This is easy! Lets find all items and their<br />

Finnish translations:<br />

// test.php<br />

Listing<br />

16-47<br />

// ...<br />

$newsItems = <strong>Doctrine</strong>_Query::create()<br />

->from('NewsItem n')<br />

->leftJoin('n.Translation t')<br />

->where('t.lang = ?')<br />

->execute(array('fi'));<br />

echo $newsItems[0]->Translation['fi']->title;<br />

The above example would produce the following output:<br />

$ php test.php<br />

joku otsikko<br />

Listing<br />

16-48<br />

NestedSet<br />

The NestedSet behavior allows you to turn your models in to a nested set tree structure<br />

where the <strong>en</strong>tire tree structure can be retrieved in one effici<strong>en</strong>t query. It also provided a nice<br />

interface for manipulating the data in your trees.<br />

Lets take a Category model for example where the categories need to be organized in a<br />

hierarchical tree structure:<br />

// models/Category.php<br />

Listing<br />

16-49<br />

class Category ext<strong>en</strong>ds <strong>Doctrine</strong>_Record<br />

{<br />

public function setTableDefinition()<br />

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

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

Saved successfully!

Ooh no, something went wrong!