I am using CakePHP 4.0.2
I have tables like below
IcsParents <–many to many–> Kids–> hasOne–>EduTypes
All associations works fines , since I can retrieve them all well.
However, when I edit IcsParents, I want to update EduTypes of Kids as well in 1 form.
The code look like this:
$icsParent = $this->IcsParents->get($id, [
'contain' => ['CourseTypes', 'Kids.EduTypes'],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$icsParent = $this->IcsParents->patchEntity($icsParent, $this->request->getData()
, ['associated' => ['Kids']]);
The $icsParent never patch the change of Kids.edu_type_id , hence nothing is updated.
I can see that the dirty property is empty.
I search this forum and google but still can’t find solution.
Below is debug output of posted data , the first record has newly entered edu_type_id to 4 , what i changed in form.
'IcsParent' => [
'kids' => [
(int) 0 => [
'edu_type_id' => '4'
],
(int) 1 => [
'edu_type_id' => '3'
]
]
],
This is patched IcsParent
object(App\Model\Entity\IcsParent) {
'id' => (int) 1,
'first_name' => 'f1-parent2',
'last_name' => 'tpln',
'mobile' => '0916825483',
'email' => 'general2000vn@yahoo.com',
'dob' => null,
'user_id' => null,
'is_mother' => false,
'address' => '',
'district_id' => null,
'ward_id' => null,
'register_status_id' => (int) 1,
'consult_status_id' => (int) 2,
'is_deleted' => false,
'is_active' => true,
'is_dupplicated' => true,
'picture' => 'parents/1.jpg',
'created' => object(Cake\I18n\FrozenTime) {
},
'modified' => object(Cake\I18n\FrozenTime) {
},
'kids' => [
(int) 0 => object(App\Model\Entity\Kid) {
'id' => (int) 1,
'first_name' => 'Fkid1',
'last_name' => 'Lkid1',
'dob' => object(Cake\I18n\FrozenDate) {},
'edu_type_id' => (int) 2,
'is_deleted' => (int) 0,
'created' => object(Cake\I18n\FrozenTime) {},
'modified' => object(Cake\I18n\FrozenTime) {},
'_joinData' => object(Cake\ORM\Entity) {
'id' => (int) 1,
'kid_id' => (int) 1,
'ics_parent_id' => (int) 1,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'KidsIcsParents'
},
'edu_type' => object(App\Model\Entity\EduType) {
'id' => (int) 2,
'name' => 'Private School',
'[new]' => false,
'[accessible]' => [
'name' => true,
'kids' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'EduTypes'
},
'[new]' => false,
'[accessible]' => [
'first_name' => true,
'last_name' => true,
'dob' => true,
'is_deleted' => true,
'created' => true,
'modified' => true,
'pupils' => true,
'ics_parents' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Kids'
},
(int) 1 => object(App\Model\Entity\Kid) {
'id' => (int) 2,
'first_name' => 'fkid2',
'last_name' => 'lkid2',
'dob' => object(Cake\I18n\FrozenDate) {},
'edu_type_id' => (int) 3,
'is_deleted' => (int) 0,
'created' => object(Cake\I18n\FrozenTime) {},
'modified' => object(Cake\I18n\FrozenTime) {},
'_joinData' => object(Cake\ORM\Entity) {
'id' => (int) 2,
'kid_id' => (int) 2,
'ics_parent_id' => (int) 1,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'KidsIcsParents'
},
'edu_type' => object(App\Model\Entity\EduType) {
'id' => (int) 3,
'name' => 'International School',
'[new]' => false,
'[accessible]' => [
'name' => true,
'kids' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'EduTypes'
},
'[new]' => false,
'[accessible]' => [
'first_name' => true,
'last_name' => true,
'dob' => true,
'is_deleted' => true,
'created' => true,
'modified' => true,
'pupils' => true,
'ics_parents' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Kids'
}
],
'course_types' => [],
'[new]' => false,
'[accessible]' => [
'first_name' => true,
'last_name' => true,
'mobile' => true,
'email' => true,
'yob' => true,
'user_id' => true,
'is_mother' => true,
'address' => true,
'district_id' => true,
'ward_id' => true,
'register_status_id' => true,
'consult_status_id' => true,
'is_deleted' => true,
'is_active' => true,
'picture' => true,
'created' => true,
'modified' => true,
'user' => true,
'district' => true,
'ward' => true,
'register_status' => true,
'consult_status' => true,
'course_types' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'IcsParents'
}
Can someone please kindly help? <— problem solved
SOLUTION: dreamingmind has pointed out for me that I need to add Kids into Accessible list in IcsParent entity