# Cakephp 3 belongsToMany association is not updating junction table

**URL:** <https://discourse.cakephp.org/t/cakephp-3-belongstomany-association-is-not-updating-junction-table/4839>\
**Category:** Need Help\
**Created:** [August 31, 2018, 9:17am UTC](https://discourse.cakephp.org/t/cakephp-3-belongstomany-association-is-not-updating-junction-table/4839 "2018-08-31T09:17:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saini.vikas630](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@saini.vikas630](https://discourse.cakephp.org/u/saini.vikas630)\
**Post date:** [August 31, 2018, 9:17am UTC](https://discourse.cakephp.org/t/cakephp-3-belongstomany-association-is-not-updating-junction-table/4839/1 "2018-08-31T09:17:17Z")

</div>

I have 4 tables: `products`, `tags`, `categories` and `taxonomy_to_products`. The table contains data that links `tags` and `products` together, same with `category` and `products`.

When I am trying to link existing category to products it’s not updating on the `taxonomy_to_products` table.

Here is my `taxonomy_to_products` table screenshot

[![enter image description here](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/7/705cc12caf188516ae2a72f6c9b9edb10225e779.png)](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/7/705cc12caf188516ae2a72f6c9b9edb10225e779.png)

Model\Table\CategoriesTable:-

```
$this->belongsToMany('Products', [
        'targetForeignKey' => 'product_id',
        'foreignKey' => 'category_id',
        'joinTable' => 'taxonomy_to_products',
        'saveStrategy' => 'replace'
    ]);

```

Model\Table\TagsTable:-

```
$this->belongsToMany('Products', [
        'targetForeignKey' => 'product_id',
        'foreignKey' => 'tag_id',
        'joinTable' => 'taxonomy_to_products',
        'saveStrategy' => 'replace'
    ]);

```

Model\Table\ProductsTable:-

```
$this->belongsToMany('Tags', [
        'foreignKey' => 'product_id',
        'targetForeignKey' => 'tag_id',
        'joinTable' => 'taxonomy_to_products',
        'saveStrategy' => 'replace'
    ]);
    $this->belongsToMany('Categories', [
        'foreignKey' => 'product_id',
        'targetForeignKey' => 'category_id',
        'joinTable' => 'taxonomy_to_products',
        'saveStrategy' => 'replace'
    ]);

```

Model\Table\TaxonomyToProducts:-

```
$this->belongsTo('Categories', [
        'foreignKey' => 'category_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Tags', [
        'foreignKey' => 'tag_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Products', [
        'foreignKey' => 'product_id',
        'joinType' => 'INNER'
    ]);

```

This is my action

```
public function edit($id = null) {
    $product = $this->Products->get($id, [
        'contain' => [
            'Media' => ['fields' => ['id', 'url']],
            'Categories',
            'Tags',
        ]
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $product = $this->Products->patchEntity($product, $this->request->getData(), ['associate' => ['Categories']]);
        //print_r($$product);exit();
        if ($this->Products->save($product)) {
            $this->Flash->success(__('The product has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The product could not be saved. Please, try again.'));
    }
    $allCategories = $this->Products->Categories->find('treeList', [
                'keyPath' => 'id',
                'valuePath' => 'name',
                'spacer' => '&#9866;',
            ])->toArray();
    $this->set(compact('product', 'allCategories'));
}

```

This is my Form:-

```
<?= $this->Form->select('categories._ids', $allCategories, ['hiddenField' => false, 'multiple' => 'checkbox', 'escape' => false])?>

```

---

<div class="post-metadata">

**Author:** ![mesiarm](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mesiarm](https://discourse.cakephp.org/u/mesiarm)\
**Post date:** [August 31, 2018, 7:27pm UTC](https://discourse.cakephp.org/t/cakephp-3-belongstomany-association-is-not-updating-junction-table/4839/2 "2018-08-31T19:27:33Z")

</div>

You should fullfile naming conventions, table should be called tags\_products. Also table isn´t well designed, category\_id should be in products table. Or if it´s true, that products has and belongs to many categories (I´m not sure about it) you should create another table categories\_products to join categories and products.
