# Saving associated data on \_joinData

**URL:** <https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334>\
**Category:** Need Help\
**Created:** [May 10, 2021, 3:54pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334 "2021-05-10T15:54:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 10, 2021, 3:54pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/1 "2021-05-10T15:54:18Z")

</div>

Hi,

want to save associated data on record  
‘Articles.\_joinData.Attachments’

when I use PatchEntity everything shows up. But when save only the joinData gets stored but not the associated records Attachments.

Anyone know what I am doing wrong there?

---

<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:** [May 10, 2021, 6:47pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/2 "2021-05-10T18:47:35Z")

</div>

It’s not clear what you’re trying to accomplish here. Can you give a little more context about what record you’re editing (guessing it’s “article”?) and what the relevant associations are?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 10, 2021, 8:24pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/3 "2021-05-10T20:24:45Z")

</div>

joinData and then a hasmany relation, but that is uploadbehavior jose gonzalez, I posted it on github there, maybe its a problem related to the behaviour?

Normally you can save \_joinData and then related records on that in one go?

---

<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:** [May 10, 2021, 11:01pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/4 "2021-05-10T23:01:11Z")

</div>

My experience is saving related records which have `_joinData`, not the other way around. Always aim to make the names of the input controls match the xpath way of getting to that field in an entity you’ve read with normal containment.

---

<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 11, 2021, 4:49am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/5 "2021-05-11T04:49:02Z")

</div>

it would be helpful, if you show the code of the form and a debug of the data before and after patching in your controller.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 9:16am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/6 "2021-05-11T09:16:21Z")

</div>

When I add the uploadbehaviour to database ArticlesAttributeAttachments the upload behaviour does not upload the file as expected. The data is provided correctly just the behaviour is not triggered somehow.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 9:57am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/7 "2021-05-11T09:57:02Z")

</div>

I can save it directly and upload it, but when I save it as part of the \_joinData.article\_attribute\_attachments nothing happens. I should test with some basic data if you can add it onto the \_joinData see if that also not work.

So the question is, are you able to add extra relational data to \_joinData?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 10:10am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/8 "2021-05-11T10:10:32Z")

</div>

As from book:

[https://book.cakephp.org/3/en/orm/saving-data.html#saving-additional-data-to-the-join-table](https://book.cakephp.org/3/en/orm/saving-data.html#saving-additional-data-to-the-join-table)

$data = [  
‘first\_name’ =\> ‘Sally’,  
‘last\_name’ =\> ‘Parker’,  
‘courses’ =\> [  
[  
‘id’ =\> 10,  
‘\_joinData’ =\> [  
‘grade’ =\> 80.12,  
‘days\_attended’ =\> 30  
]  
],  
// Other courses.  
]  
];  
$student = $this-\>Students-\>newEntity($data, [  
‘associated’ =\> [‘Courses.\_joinData’]  
]);

Now you have hasMany relation on the StudentsCourses table to let’s say images.  
Then you want to save it in one go:

$data = [  
‘first\_name’ =\> ‘Sally’,  
‘last\_name’ =\> ‘Parker’,  
‘courses’ =\> [  
[  
‘id’ =\> 10,  
‘\_joinData’ =\> [  
‘grade’ =\> 80.12,  
‘days\_attended’ =\> 30  
‘images’ =\> [‘0’ =\> image …]  
]  
],  
// Other courses.  
]  
];

$student = $this-\>Students-\>newEntity($data, [  
‘associated’ =\> [‘Courses.\_joinData.Images’]  
]);

This is what I am trying but it seems to not work. And I wonder if it is at all possible. But I have only tried with Images having the upload behaviour attached to it. So I can test with a regular table. But that’s why I am asking it, to see if anybody knows if it is possible to do that?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 10:33am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/9 "2021-05-11T10:33:10Z")

</div>

I can do patchEntity and have all the data show correctly, but when saving it, the Images do not get saved.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 11, 2021, 3:18pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/10 "2021-05-11T15:18:53Z")

</div>

Not having seen your code, I will venture a guess.

You say

> [@thomasg](#):
>
> I can do patchEntity and have all the data show correctly

I find it very easy to look at debugs of patch data and not recognize that a deep level is still in array form when it should have been converted to an entity.

You may not be setting the `associated` key of your `patchEntity()` and `save()` calls properly to get your deeply nested data included.

The thing you’re trying to do is possible.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 3:34pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/11 "2021-05-11T15:34:11Z")

</div>

Based on example of cookbook, how would you write that then?

$student = $this-\>Students-\>newEntity($data, [  
‘associated’ =\> [‘Courses.\_joinData.Images’]  
]);

$this-\>Students-\>save($student);

??

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 11, 2021, 5:17pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/12 "2021-05-11T17:17:54Z")

</div>

Yes. And I would expect you would need the key on the save also:

```php
$student = $this->Students->newEntity(
   $data, 
   [‘associated’ => [‘Courses._joinData.Images’]]
);

$this->Students->save(
   $student, 
   [‘associated’ => [‘Courses._joinData.Images’]]
);

```

There is more discussion on [this page](https://book.cakephp.org/3/en/orm/saving-data.html). For example, the [4th code example here](https://book.cakephp.org/3/en/orm/saving-data.html#converting-request-data-into-entities) or [in this section](https://book.cakephp.org/3/en/orm/saving-data.html#saving-associations) and others below it.

Note the array syntax they use as an alternative to the dot syntax you illustrated.

---

<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:** [May 11, 2021, 7:19pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/13 "2021-05-11T19:19:12Z")

</div>

Am I the only one that’s still confused by the presence of a further association on the `_joinData`? Is that a normal thing that I’ve just not come across before? In the structure under discussion, I’d expect that `_joinData` is an entity for the `belongsToMany` join table between Students and Courses. That join table itself then has a `hasMany` relationship to Images?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 11, 2021, 9:02pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/14 "2021-05-11T21:02:14Z")

</div>

It’s not a structure or approach that I’ve used but it is an [example straight from the book](https://book.cakephp.org/3/en/orm/saving-data.html#saving-additional-data-to-the-join-table) for use when the join table carries data beyond the two foreign-keys.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 9:02pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/15 "2021-05-11T21:02:57Z")

</div>

i will check it again tomorow, i tried it before like that with the associated on the save as well. Maybe had a mistake in there…

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 11, 2021, 9:09pm UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/16 "2021-05-11T21:09:48Z")

</div>

In that example the image could be a certificate that gets awarded on completion of the course…

I am using it for something else like attributes that you can attach to an article, and one kind of attribute is a file, so I want to attach the file to that record…

---

<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 12, 2021, 4:47am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/17 "2021-05-12T04:47:27Z")

</div>

> [@thomasg](#):
>
> So the question is, are you able to add extra relational data to \_joinData?

I know that it works with at least on a hasone association.  
In one project I have the tables:  
dishes (id, name, created, modified)  
dishsizes (id, name,…)  
currencies (id, name…)  
and the join Table:  
dishes\_dishsizes (id, dish\_id, dishsize\_id, unitprice(dec 15,2), currency\_id)

The join Table hasone currencies and on adding or editing a dish I can choose the currency for each dishsize.

I never tested it with fileupload and don’t know the uploadplugin you are using.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 12, 2021, 8:43am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/18 "2021-05-12T08:43:25Z")

</div>

The problem is to create a new record (in your case would be create a new currency record). Not to select the id and set it.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 12, 2021, 8:44am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/19 "2021-05-12T08:44:59Z")

</div>

I tried it again with save ‘associated’ =\> … and it does not work like that. I can see the entity object is set correctly it just not gets saved to the database.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 12, 2021, 9:23am UTC](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334/20 "2021-05-12T09:23:52Z")

</div>

== removed this because it worked to my additional code

[Next page](https://discourse.cakephp.org/t/saving-associated-data-on-joindata/9334.md?page=2)
