Skip to content

Commit b6e1f57

Browse files
committed
[Tree] Add doc for TreeObjectHydrator
1 parent 7d0201f commit b6e1f57

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

doc/tree.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Thanks for contributions to:
2222
- **[everzet](http://github.com/everzet) Kudryashov Konstantin** for TreeLevel implementation
2323
- **[stof](http://github.com/stof) Christophe Coevoet** for getTreeLeafs function
2424

25+
Update **2017-04-22**
26+
27+
- Added the `TreeObjectHydrator` class for building trees from entities
28+
2529
Update **2012-06-28**
2630

2731
- Added "buildTree" functionality support for Closure and Materialized Path strategies
@@ -636,6 +640,31 @@ $controller = $this;
636640

637641
<a name="advanced-examples"></a>
638642

643+
## Building trees from your entities
644+
645+
You can use the `childrenHierarchy` method to build an array tree from your result set.
646+
However, sometimes it is more convenient to work with the entities directly. The `TreeObjectHydrator`
647+
lets you build a tree from your entities instead, without triggering any more queries.
648+
649+
First, you have to register the hydrator in your Doctrine entity manager.
650+
651+
```php
652+
<?php
653+
$em->getConfiguration()->addCustomHydrationMode('tree', 'Gedmo\Tree\Hydrator\ORM\TreeObjectHydrator');
654+
```
655+
656+
The hydrator requires the `HINT_INCLUDE_META_COLUMNS` query hint. Without it the hydrator will not work!
657+
Other than that, the usage is straight-forward.
658+
659+
```php
660+
<?php
661+
$repo = $em->getRepository('Entity\Category');
662+
663+
$tree = $repo->createQueryBuilder('node')->getQuery()
664+
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
665+
->getResult('tree');
666+
```
667+
639668
## Advanced examples:
640669

641670
### Nesting Translatable and Sluggable extensions

0 commit comments

Comments
 (0)