# Contain() in Table::beforeFind() sometimes causes issues in Cakephp4 (especially with Muffin/SlugBehavior)

**URL:** <https://discourse.cakephp.org/t/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior/10785>\
**Category:** Need Help\
**Created:** [November 24, 2022, 10:15am UTC](https://discourse.cakephp.org/t/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior/10785 "2022-11-24T10:15:28Z")\
**Posts on this page:** 3\
**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:** [November 24, 2022, 10:15am UTC](https://discourse.cakephp.org/t/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior/10785/1 "2022-11-24T10:15:28Z")

</div>

Hi,

I have the following association : `Products` hasMany `Photos`.  
My `ProductsTable` uses `Muffin/SlugBehavior` to build a unique slug on products.

In order to automatically get `Photos` related to a `Product` each time I find a `Product`, I made `contain()` in `ProductsTable::beforefind()`. I need `contain()` not in Controller.

Here is my `ProductsTable` :

```php
// ProductsTable
class ProductsTable extends Table
{
   public function initialize(array $config): void
   {
      $this->hasMany('Photos');

      $this->addBehavior('Muffin/Slug.Slug', [
         'displayField' => 'title', // 'title' is the field I want to build a unique slug from.
      ]);
   }

   public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
   {
      $query->contain('Photos');
      return $query;
   }
}

```

In most cases it works well, but **an issue happens when I try to create a Product with an already existing title (that will generate an existing slug)**.

I have the following `InvalidArgumentException` : Unable to load Photos association. Ensure foreign key in Products is selected. The exception is thrown in `Muffin/SlugBehavior::_uniqueSlug()` at the line `$this->_table->exists($conditions)`.

I don’t understand exactly what’s the problem…

Could anybody tell me how to fix it ?

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [November 28, 2022, 8:34am UTC](https://discourse.cakephp.org/t/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior/10785/2 "2022-11-28T08:34:52Z")

</div>

Fetching the related Photos requires the primary query to select the `id` field. `Table::exists()` sets a custom fields list which doesn’t include the `id` field which is what’s causing your problem.

The solution is to either:

1. Add `$query->enableAutoField()` in your `beforeFind()` so that the required field auto selected for your contain to work or use a custom.
2. Instead of `beforeFind()` use a custom finder which contains the Photos.

---

<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:** [November 28, 2022, 5:09pm UTC](https://discourse.cakephp.org/t/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior/10785/3 "2022-11-28T17:09:15Z")

</div>

> [@ADmad](#):
>
> Add `$query->enableAutoFields()` in your `beforeFind()`

Thanks for the tip !
