# Best practice for accessing additional data (many to many)

**URL:** <https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805>\
**Category:** Need Help\
**Created:** [March 18, 2019, 7:32pm UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805 "2019-03-18T19:32:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mo86](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mo86/32/92_2.png) [@mo86](https://discourse.cakephp.org/u/mo86)\
**Post date:** [March 18, 2019, 7:32pm UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/1 "2019-03-18T19:32:26Z")

</div>

Hi folks,

I got a question regarding “joinData” in a belongsToMany (many to many) relation.

Lets assume we have 3 tables  
ATable, BTable and ABTable

ABTable contains some additional data for example a **price** value.

ATable defines $this-\>belongsToMany(‘B’, [‘through’ =\> ‘AB’]);  
BTable defines $this-\>belongsToMany(‘A’, [‘through’ =\> ‘AB’]);

We also have an own **entity** class for each table:  
A, B and AB.

Now I try this:

Get an A entity  
$a = $this-\>A-\>get(1, [‘contain’ =\> [‘B’]])

Than dd($a-\>b[0]); would return something like this

…

‘\_joinData’ =\> object(Cake\ORM\Entity) {  
…  
‘price’ =\> (float) 123,  
‘[repository]’ =\> ‘AB’  
…

My question is why I am not getting an instance of “App\Model\Entity\AB” instead of the “\_joinData”?  
Is this the normal cakephp behaviour for accesing the additional stored data in an many to many table?

Best regards

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 19, 2019, 2:47am UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/2 "2019-03-19T02:47:56Z")

</div>

I don’t think you should have the “Table” at the end of your `through` key. Try just `'through' => 'AB'`.

---

<div class="post-metadata">

**Author:** ![mo86](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mo86/32/92_2.png) [@mo86](https://discourse.cakephp.org/u/mo86)\
**Post date:** [March 19, 2019, 6:06pm UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/3 "2019-03-19T18:06:57Z")

</div>

Thanks you are right. I had it right in my code but did it wrong here in my example. But it still doesnt work.  
The “joinData” key is still there and it is not converted into an Entity of AB.

I think this is the normal cake behaviour but is there any way to convert the join data automaticly into the belonging entity?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 20, 2019, 12:36am UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/4 "2019-03-20T00:36:56Z")

</div>

Just checked mine, and the `_joinData` is indeed the specific entity type, not a generic one. I’d double-check your file and class naming structure to make sure that everything is as it should be (singular vs plural, in the right namespaces, etc.). You might want to inspect the return values of things like `TableRegistry::get('AB')`, `TableRegistry::get('AB')->newEntity()` and `new \App\Model\Entity\AB()`, those might help you quickly figure out which piece of the puzzle is wrong.

---

<div class="post-metadata">

**Author:** ![mo86](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mo86/32/92_2.png) [@mo86](https://discourse.cakephp.org/u/mo86)\
**Post date:** [March 20, 2019, 8:04am UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/5 "2019-03-20T08:04:39Z")

</div>

Thank you I will try it later 🙂

---

<div class="post-metadata">

**Author:** ![mo86](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mo86/32/92_2.png) [@mo86](https://discourse.cakephp.org/u/mo86)\
**Post date:** [March 21, 2019, 7:12am UTC](https://discourse.cakephp.org/t/best-practice-for-accessing-additional-data-many-to-many/5805/6 "2019-03-21T07:12:04Z")

</div>

YOu were right. A single get() to the AB Table does also return a generic Entity. I can set it in ABTable with $this-\>setEntityClass(’…’); and it works.  
And now I also found out that I had the wrong Entity name to make it work out of the box 🙂 Thanks for your help!
