# Cake4: belongstomany on same table (solved)

**URL:** <https://discourse.cakephp.org/t/cake4-belongstomany-on-same-table-solved/9314>\
**Category:** Need Help\
**Created:** [May 3, 2021, 12:25pm UTC](https://discourse.cakephp.org/t/cake4-belongstomany-on-same-table-solved/9314 "2021-05-03T12:25:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [May 3, 2021, 12:25pm UTC](https://discourse.cakephp.org/t/cake4-belongstomany-on-same-table-solved/9314/1 "2021-05-03T12:25:53Z")

</div>

I have 3 tables

- producttypes (id, name); The table is filled with values “Product” and “Ingredient”
- products (id, name, producttype\_id)
- ingredients\_products (id, ingredient\_id, product\_id) (ingredient\_id comes from table products, product\_id comes from table products)

A Product can be “Product” or “Ingredient”.

I want to get a belongstomany Relation on Products, where a product belongstomany products.

In ProductsTable i added:

> ```
> $this->belongsToMany('IngredientsProducts', [
> 'joinTable' => 'ingredients_products',
> 'foreignKey' => 'product_id',
> 'targetForeignKey' => 'ingredient_id'
> 
> ```

In entity products i added:

> ‘ingredientsproducts’=\>true,`

In ProductsController I select the ingredients:

> $ingredients=$this-\>Products-\>find(‘list’,[  
> ‘conditions’=\>[‘Products.salespoint\_id’=\>$this-\>request-\>getSession()-\>read(‘salespoint\_id’),‘Products.producttype\_id’=\>2],  
> ]);

In add Form i have:

> echo $this-\>Form-\>control(‘ingredientsproducts.\_ids’, [‘options’ =\> $ingredients,‘type’=\>‘select’,‘multiple’=\>‘checkbox’,‘hiddenField’=\>false,‘label’=\>false]);

After submitting the form I get this request-\>data

```
[
'name' => 'Product with ingredient', 
'ordernumber' => '',
 'description' => '',
 'productcategory_id' => '',
 'gtin' => '',
 'allergens' => [
   '_ids' => [
       (int) 0 => '1',
       (int) 1 => '2',
],
], 
'ingredientsproducts' => [
   '_ids' => [
      (int) 0 => '2',
],
], 
'image_file' => object(Laminas\Diactoros\UploadedFile) id:0 { },
]

```

After patching, the entity looks like this (I show only the relevant parts; hasErrors is false):

```
'allergens' => [
   (int) 0 => object(App\Model\Entity\Allergen) id:1 { }, 
   (int) 1 => object(App\Model\Entity\Allergen) id:4 { },
] 
'ingredientsproducts' => [
   '_ids' => [
     (int) 0 => '2',
],

```

So it seems, that I something completely did not understood on associating table together.  
Can someone maybe help me to associate the table products with itself?  
If more information is needed, please let me know.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [May 3, 2021, 2:03pm UTC](https://discourse.cakephp.org/t/cake4-belongstomany-on-same-table-solved/9314/2 "2021-05-03T14:03:46Z")

</div>

Got it working.  
Finally I have in ProductsTable:

```
    $this->belongsToMany('Ingredients', [
        'joinTable' => 'ingredients_products',
        'className' => 'Products',
    ]);

```

In entity product.php

> ‘ingredients’=\>true,

And changed the input field in the form to

> echo $this-\>Form-\>control(‘ingredients.\_ids’, [‘options’ =\> $ingredients,‘type’=\>‘select’,‘multiple’=\>‘checkbox’,‘hiddenField’=\>false,‘label’=\>false]);
