# How to remove \`contain()\` added to the query by a behavior in Cakephp 4?

**URL:** <https://discourse.cakephp.org/t/how-to-remove-contain-added-to-the-query-by-a-behavior-in-cakephp-4/10055>\
**Category:** Need Help\
**Created:** [January 6, 2022, 6:24pm UTC](https://discourse.cakephp.org/t/how-to-remove-contain-added-to-the-query-by-a-behavior-in-cakephp-4/10055 "2022-01-06T18:24:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliv](https://avatars.discourse-cdn.com/v4/letter/o/45deac/32.png) [@Oliv](https://discourse.cakephp.org/u/Oliv)\
**Post date:** [January 6, 2022, 6:24pm UTC](https://discourse.cakephp.org/t/how-to-remove-contain-added-to-the-query-by-a-behavior-in-cakephp-4/10055/1 "2022-01-06T18:24:01Z")

</div>

**I’m looking for the way to do an equivalent to cakephp2’s `'recursive' => -1` on a `find()`.**

I have a Model `Categories` that uses the `TreeBehavior`.

In my controller I get the path of the category :

```php
// in CategoriesController.php
$path = $this->Categories->find('path', ['for' => $id]);

```

But I have the error message **`Unable to load Photos association. Ensure foreign key in Categories is selected`**

The problem is that `find('path')` above seems to try to find as well associated Model to `Categories` as `Categories` uses another behavior (`AttachmentBehavior`) that adds `contain('Photos')` :

```php
// in AttachmentBehavior (that is used by `CategoriesTable`)
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
    $query->contain('Photos');

    return $query;
}

```

How can I remove `contain()` in `$this->Categories->find('path', ['for' => $id])`.  
Is there an option ?

I tried `$this->Categories->find('path', ['for' => $id, 'contain' => [])` but I have the same error…

---

<div class="post-metadata">

**Author:** ![Oliv](https://avatars.discourse-cdn.com/v4/letter/o/45deac/32.png) [@Oliv](https://discourse.cakephp.org/u/Oliv)\
**Post date:** [January 7, 2022, 11:44am UTC](https://discourse.cakephp.org/t/how-to-remove-contain-added-to-the-query-by-a-behavior-in-cakephp-4/10055/2 "2022-01-07T11:44:24Z")

</div>

In fact, I understand that `Treebehavior::findPath()` selects only `lft` and `rght` fields (for the categorie’s node) and not the foreign key as my `AttachmentBehavior` needs.

I made the choice to make `contain()` in my `AttachmentBehavior::beforeFind()` as there’s in fact many dynamic associations done in that method (not only ‘Photos’, ‘Photos’ was for example).

My `AttachmentBehavior::beforeFind()` looks like that :

```php
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
    foreach ($this->_fields as $field => $value) { 
        $query->contain($value['alias']); // How to make sure the foreign key is present ?
    }
    return $query;
}

```

Is there a way to modify my `AttachmentBehavior::beforeFind()` to make sure the foreign key is present ?
