patchEntity() on associated models always creating new (associated model id is not preserved)


#1

Hi,

I am trying to use patchentity to ‘update’ data in an associated model but when it patches it seems to create a new associated entity and NOT preserve the old entity. When the associated data is unchanged is unchanged all the members get marked as dirty and the original id is dropped.

I cannot figure out what is going wrong.

If I load the associated entity on its own and then patch it will maintain all the data and also recognize the correct changes to the members. But this behaviour is not consistent with patchEntity().

What am I doing wrong?

Here is the retrieved Entity with association:

App\Model\Entity\Member {#154
+“id”: 779
+“members_teams”: array:1 [
0 => App\Model\Entity\MembersTeam {#161
+“id”: 3234
+“member_id”: 779
+“team_id”: 82
+“squad_number”: null
+“season_id”: 4
+“status_id”: 1
+“playingposition_id”: null
+“inactive”: false
+“created”: Cake\I18n\FrozenTime {#164
+“time”: “2018-04-07T08:37:46+10:00”
+“timezone”: “Australia/Brisbane”
+“fixedNowTime”: false
}
+“modified”: Cake\I18n\FrozenTime {#168
+“time”: “2018-04-07T09:33:33+10:00”
+“timezone”: “Australia/Brisbane”
+“fixedNowTime”: false
}
+"[new]": false
+"[accessible]": array:1 [
" => true
]
+"[dirty]": []
+"[original]": []
+"[virtual]": []
+"[errors]": []
+"[invalid]": []
+"[repository]": “MembersTeams”
}
]
+"[new]": false
+"[accessible]": array:1 [
"
” => true
]
+"[dirty]": []
+"[original]": []
+"[virtual]": array:2 [
0 => “FullName”
1 => “Age”
]
+"[errors]": []
+"[invalid]": []
+"[repository]": “Members”
}

Here is the associated entity data to check/change:

array:1 [
“members_teams” => array:1 [
0 => array:4 [
“member_id” => 779
“team_id” => 82
“season_id” => “4”
“status_id” => “1”
]
]
]

I then patch the original Entity (Member) with the array and the associated array as such:

$member = $MembersTable->patchEntity($member, $memberdata, [‘associated’ => [‘MembersTeams’]]);

Here is the patched entity which has created a new associated entity (MembersTeam) and moved the original to a key marked ‘original’ even though the MembersTeam entity data has not changed.

App\Model\Entity\Member {#154
+“id”: 779
+“members_teams”: array:1 [
0 => App\Model\Entity\MembersTeam {#229
+“member_id”: 779
+“team_id”: 82
+“season_id”: 4
+“status_id”: 1
+"[new]": true
+"[accessible]": array:1 [
" => true
]
+"[dirty]": array:4 [
“member_id” => true
“team_id” => true
“season_id” => true
“status_id” => true
]
+"[original]": []
+"[virtual]": []
+"[errors]": []
+"[invalid]": []
+"[repository]": “MembersTeams”
}
]
+"[new]": false
+"[accessible]": array:1 [
"
” => true
]
+"[dirty]": array:1 [
“members_teams” => true
]
+"[original]": array:1 [
“members_teams” => array:1 [
0 => App\Model\Entity\MembersTeam {#161
+“id”: 3234
+“member_id”: 779
+“team_id”: 82
+“squad_number”: null
+“season_id”: 4
+“status_id”: 1
+“playingposition_id”: null
+“inactive”: false
+“created”: Cake\I18n\FrozenTime {#164
+“time”: “2018-04-07T08:37:46+10:00”
+“timezone”: “Australia/Brisbane”
+“fixedNowTime”: false
}
+“modified”: Cake\I18n\FrozenTime {#168
+“time”: “2018-04-07T09:33:33+10:00”
+“timezone”: “Australia/Brisbane”
+“fixedNowTime”: false
}
+"[new]": false
+"[accessible]": array:1 [
“*” => true
]
+"[dirty]": []
+"[original]": []
+"[virtual]": []
+"[errors]": []
+"[invalid]": []
+"[repository]": “MembersTeams”
}
]
]
+"[virtual]": array:2 [
0 => “FullName”
1 => “Age”
]
+"[errors]": []
+"[invalid]": []
+"[repository]": “Members”
}


#2

here you have got 2 IDs and I think that this is a problem.
I think Team id should be id not team_id