# How to use cakephp 3 associated function

**URL:** <https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426>\
**Category:** Need Help\
**Created:** [June 20, 2018, 9:03am UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426 "2018-06-20T09:03:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamalbhojwani93](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kamalbhojwani93](https://discourse.cakephp.org/u/kamalbhojwani93)\
**Post date:** [June 20, 2018, 9:03am UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426/1 "2018-06-20T09:03:47Z")

</div>

I am not able to save associated data in table…

**My Controller**

```
    public function add() {
		$venue = $this->Venues->newEntity();
		if ($this->request->is('post')) {
			// $associated = ['associated' => ['VenuesTypes']];
			$venue = $this->Venues->patchEntity($venue, $this->request->data);
		    $venue->user_id = $this->Auth->user('id');
			pr($venue);
			exit();
			if ($result = $this->Venues->save($venue)) {
    			$this->copyVenueImages($result->id);                
    			$this->Flash->success(__('The venue has been saved.'));
    			return $this->redirect(['action' => 'index']);
			}
		    $this->Flash->error(__('The venue could not be saved. Please, try again.'));
		}

		// $category = $this->Venues->EventCatagories->find('list');
		$types = $this->Venues->Types->find('list');
		// $amenities = $this->Venues->Amenities->find('list');
		$countries = $this->Venues->Countries->find('list');
		$this->set(compact('venue', 'category', 'countries', 'types'));
		$this->set('imgId', uniqid());
	}

```

**Venue Model**

```
// Venues Types
    $this->belongsToMany('VenuesTypes', [
        'foreignKey' => 'venues_id',
        'targetForeignKey' => 'types_id',
        'joinTable' => 'venues_types'
    ]);

    $this->hasMany('Types', [
        'joinTable' => 'venues_types',
    ]);

//Types Model
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('types');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('VenuesTypes', [
        'foreignKey' => 'types_id'
    ]);
}

// VenuesTypes Model
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('venues_types');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Venues', [
        'foreignKey' => 'venues_id'
    ]);
    $this->belongsTo('Types', [
        'foreignKey' => 'types_id'
    ]);
}

```

Result data :-  
App\Model\Entity\Venue Object  
(  
[title] =\> Costumes  
[contact\_person] =\>  
[email] =\>  
[phone\_no] =\>  
[mobile\_no] =\>  
[address] =\>  
[city] =\>  
[state] =\>  
[zipcode] =\>  
[latitude] =\>  
[longitude] =\>  
[venues\_types] =\> Array  
(  
[\_ids] =\> Array  
(  
[0] =\> 2  
[1] =\> 5  
[2] =\> 10  
[3] =\> 13  
)

```
    )

[setting] => 
[capacity] => 
[website] => 
[user_id] => 1
[[new]] => 1
[[accessible]] => Array
    (
        [title] => 1
        [slug] => 1
        [user_id] => 1
        [contact_person] => 1
        [email] => 1
        [website] => 1
        [address] => 1
        [country_id] => 1
        [state] => 1
        [city] => 1
        [zipcode] => 1
        [latitude] => 1
        [longitude] => 1
        [phone_no] => 1
        [mobile_no] => 1
        [image] => 1
        [created] => 1
        [modified] => 1
        [country] => 1
        [capacity] => 1
        [setting] => 1
        [venue_categories] => 1
        [venues_types] => 1
        [venues_amenities] => 1
        [venue_enqueries] => 1
    )

[[dirty]] => Array
    (
        [title] => 1
        [contact_person] => 1
        [email] => 1
        [phone_no] => 1
        [mobile_no] => 1
        [address] => 1
        [city] => 1
        [state] => 1
        [zipcode] => 1
        [latitude] => 1
        [longitude] => 1
        [venues_types] => 1
        [setting] => 1
        [capacity] => 1
        [website] => 1
        [user_id] => 1
    )

[[original]] => Array
    (
    )

[[virtual]] => Array
    (
    )

[[errors]] => Array
    (
    )

[[invalid]] => Array
    (
    )

[[repository]] => Venues

```

)

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [June 20, 2018, 9:21am UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426/2 "2018-06-20T09:21:30Z")

</div>

What does your form data look like?

---

<div class="post-metadata">

**Author:** ![kamalbhojwani93](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kamalbhojwani93](https://discourse.cakephp.org/u/kamalbhojwani93)\
**Post date:** [June 20, 2018, 1:25pm UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426/3 "2018-06-20T13:25:42Z")

</div>

Result Like:  
App\Model\Entity\Venue Object  
(  
[title] =\> demouser  
[contact\_person] =\>  
[email] =\>  
[phone\_no] =\>  
[mobile\_no] =\>  
[address] =\>  
[city] =\>  
[state] =\>  
[zipcode] =\>  
[latitude] =\>  
[longitude] =\>  
[venues\_types] =\> Array  
(  
[\_ids] =\> Array  
(  
[0] =\> 2  
[1] =\> 5  
[2] =\> 7  
)

```
    )

[setting] => 
[capacity] => 
[website] => 
[user_id] => 1
[[new]] => 1
[[accessible]] => Array
    (
        [title] => 1
        [slug] => 1
        [user_id] => 1
        [contact_person] => 1
        [email] => 1
        [website] => 1
        [address] => 1
        [country_id] => 1
        [state] => 1
        [city] => 1
        [zipcode] => 1
        [latitude] => 1
        [longitude] => 1
        [phone_no] => 1
        [mobile_no] => 1
        [image] => 1
        [created] => 1
        [modified] => 1
        [country] => 1
        [capacity] => 1
        [setting] => 1
        [venue_categories] => 1
        [venues_types] => 1
        [venues_amenities] => 1
        [venue_enqueries] => 1
    )

[[dirty]] => Array
    (
        [title] => 1
        [contact_person] => 1
        [email] => 1
        [phone_no] => 1
        [mobile_no] => 1
        [address] => 1
        [city] => 1
        [state] => 1
        [zipcode] => 1
        [latitude] => 1
        [longitude] => 1
        [venues_types] => 1
        [setting] => 1
        [capacity] => 1
        [website] => 1
        [user_id] => 1
    )

[[original]] => Array
    (
    )

[[virtual]] => Array
    (
    )

[[errors]] => Array
    (
    )

[[invalid]] => Array
    (
    )

[[repository]] => Venues

```

)

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [June 20, 2018, 1:26pm UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426/4 "2018-06-20T13:26:57Z")

</div>

I believe your issue is

```auto
    $this->belongsToMany('VenuesTypes', [
        'foreignKey' => 'venues_id',
        'targetForeignKey' => 'types_id',
        'joinTable' => 'venues_types'
    ]);

```

You can’t have a belongsToMany that uses the same table for the target and the join table. They should be different.

---

<div class="post-metadata">

**Author:** ![kamalbhojwani93](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kamalbhojwani93](https://discourse.cakephp.org/u/kamalbhojwani93)\
**Post date:** [June 22, 2018, 5:11am UTC](https://discourse.cakephp.org/t/how-to-use-cakephp-3-associated-function/4426/5 "2018-06-22T05:11:05Z")

</div>

Can you please help me out from this?
