Saving associated data on _joinData

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?

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?

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?

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.

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.

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.

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?

As from book:

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?

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

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

You say

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.

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

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

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

??

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

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

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

There is more discussion on this page. For example, the 4th code example here or in this section and others below it.

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

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?

It’s not a structure or approach that I’ve used but it is an example straight from the book for use when the join table carries data beyond the two foreign-keys.

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…

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…

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.

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.

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.

== removed this because it worked to my additional code