# Associating \_joinData

**URL:** <https://discourse.cakephp.org/t/associating-joindata/4178>\
**Category:** Need Help\
**Created:** [April 16, 2018, 12:20pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178 "2018-04-16T12:20:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 16, 2018, 12:20pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/1 "2018-04-16T12:20:46Z")

</div>

So i have 2 tables with both having association belongsToMany to each other. The join table between these 2 has some additional fields and one of them is foreign key to third table. My question is how can i load this association of \_jointData between these 2 tables?

Example:

Table Articles belongs to many Tags and Tags belongs to many Articles. Join table for both is ArticlesTags which has columns: article\_id, tag\_id and additional field image\_id which is connected with Images table. When in controller, how can I conatin() so that \_joinData in view contains not just image\_id but the Image object as well?

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [April 16, 2018, 3:54pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/2 "2018-04-16T15:54:10Z")

</div>

just use normal contain e.g.

Articles BelongsToMany Tags  
Tags BelongsToMany Articles.

just use Articles-\>find()-\>contain([‘Tags’]);

and to get join data

$data-\>tags-\>\_joinData-\>data\_from\_join\_data

---

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 16, 2018, 4:08pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/3 "2018-04-16T16:08:11Z")

</div>

I’m afraid you misunderstood my question… if i do like you said I’m gonna have \_joinData and be able to access it like you said but i will only have accessible image\_id and not image entity with all data. Your line `Articles->find()->contain([‘Tags’])` will give me array of Tag entities for that article but not the image entity from \_joinData which represents repository ArticlesTags.

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [April 16, 2018, 4:17pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/4 "2018-04-16T16:17:24Z")

</div>

how many table you trying to join??

where you planing to place the image??

---

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 16, 2018, 4:24pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/5 "2018-04-16T16:24:37Z")

</div>

![51](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/cb5b724d3f715a63aa527f0ba4caad5753209dd8.png)

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [April 16, 2018, 4:26pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/6 "2018-04-16T16:26:41Z")

</div>

have you try to use

$data-\>Articles-\>find()-\>contain([‘Tags’, ‘Images’])??

that how you access it. From Articles, Tags, Images, or directly from ArticlesTags.

example :  
from PostsController that not related with image

```
$this->loadModel('Images'); or
$this->loadModel('ArticlesTags');

$this->Images->find(); or 
$this->ArticlesTags->find()->contain('Images');
```

---

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 16, 2018, 4:32pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/7 "2018-04-16T16:32:48Z")

</div>

No it doesnt work that way… this would only contain images if articles had image\_id field

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [April 16, 2018, 4:45pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/8 "2018-04-16T16:45:35Z")

</div>

now you requirement is different from your qestion.

so you want the article to load the image event it doesn’t have image, what image it going to load if it doesn’t have an image??

---

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 16, 2018, 4:53pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/9 "2018-04-16T16:53:48Z")

</div>

Hmm let me try to reformulate the question: Imagine that articles have images like you said, but also imagine that this jointable `ArticlesTags` has Images as well… which are not the same. I need the image object from `ArticlesTags` table… not the Articles table? Check the screenshot above… i need the images from middle table, not the left one. (they are not the same)

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [April 16, 2018, 5:09pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/10 "2018-04-16T17:09:27Z")

</div>

owhh… i see you the image object loaded inside \_joinData, right?

to what i know \_joinData will only contain the data of joinTable, article\_id, tag\_id, image\_id. with some extra field to keep the data inside the joinTable it self or if you need to use join columns outside of the conventions, e.g (HasMany Through)

documentation to refer to  
[https://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option](https://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option)

so the answer is, you cant do that. IMHO

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [April 16, 2018, 5:53pm UTC](https://discourse.cakephp.org/t/associating-joindata/4178/11 "2018-04-16T17:53:53Z")

</div>

it should be possible using `through` option as @donnidani said but you would have to alias table

```auto
$this->belongsToMany('ArticlesTagsImages', [
     'classname' => 'Images', 
     'through' => 'ArticlesTags'
]);

```

and then you could

```auto
$this->Articles->find()
->contain([
   'ArticlesTagsImages',
   'Tags'
]);

```

or you could create another table/model and have it `belongsTo` at those 3 tables (images, tags, articles) and then call it

```auto
$this->Articles->find()
->contain([
    'ArticlesTags' => [
        'Image',
        'Tag'
]);

```

---

<div class="post-metadata">

**Author:** ![itodor](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@itodor](https://discourse.cakephp.org/u/itodor)\
**Post date:** [April 18, 2018, 8:33am UTC](https://discourse.cakephp.org/t/associating-joindata/4178/12 "2018-04-18T08:33:52Z")

</div>

Ok fixed it with help of this post: [Trying to get additional data using BelongsToMany with multiple tables](https://discourse.cakephp.org/t/trying-to-get-additional-data-using-belongstomany-with-multiple-tables/3699/3)

Thx everyone for help
