# Working with Tree and Contain

**URL:** <https://discourse.cakephp.org/t/working-with-tree-and-contain/8845>\
**Category:** Need Help\
**Created:** [January 7, 2021, 4:52pm UTC](https://discourse.cakephp.org/t/working-with-tree-and-contain/8845 "2021-01-07T16:52:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinkbug](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@justinkbug](https://discourse.cakephp.org/u/justinkbug)\
**Post date:** [January 7, 2021, 4:52pm UTC](https://discourse.cakephp.org/t/working-with-tree-and-contain/8845/1 "2021-01-07T16:52:19Z")

</div>

This is probably an easy one for someone out there. I’m just having trouble getting it all put together. I’m trying to pull tree data with assoicated data that also has tree structure.

```
        $navigation = $this->find('children', [
            'for' => $id
        ])
        ->find('threaded')
        ->contain(['Links'])
        ->toArray()
        ;

```

Links has Tree behavior as well and I need it to be sorted by the lft column.

---

<div class="post-metadata">

**Author:** ![justinkbug](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@justinkbug](https://discourse.cakephp.org/u/justinkbug)\
**Post date:** [January 7, 2021, 5:41pm UTC](https://discourse.cakephp.org/t/working-with-tree-and-contain/8845/2 "2021-01-07T17:41:42Z")

</div>

Just as an example I did this in CakePHP2 like so:

```
        $navigation = $this->find('threaded', array(
            'conditions' => array(
                'Menu.lft BETWEEN ? AND ?' => array($menu['Menu']['lft'], $menu['Menu']['rght'])
            ),
            'order' => 'lft',
            'contain' => array('Link' => array(
                'order' => 'lft'
            ))
        ));
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 7, 2021, 7:00pm UTC](https://discourse.cakephp.org/t/working-with-tree-and-contain/8845/3 "2021-01-07T19:00:01Z")

</div>

To add to the query that’s run for the contained table, look at the [queryBuilder](https://book.cakephp.org/3/en/orm/retrieving-data-and-resultsets.html#passing-conditions-to-contain) option. There are a few usage examples on that page, including one about `order`.
