Hello,
I would really appreciate if somebody could help me with this.
I have 2 tables (suppose students and student_courses table) that has ‘has many’ association. When I try to add data, It adds data only in students table and doesn’t add any data in student_courses table. It doesn’t show any error also.
Here is the Controller code:
public function add()
{
$student = $this->Studnets->newEmptyEntity();
if ($this->request->is(‘post’)) {
$student = $this->Studnets->patchEntity($student, $this->request->getData(),[
‘associated’ => [
‘StudentCourses’]
]);
if ($this->Studnets->save($student )) {
$this->Flash->success((‘The student data has been saved.’));
return $this->redirect([‘action’ => ‘index’]);
}
$this->Flash->error((‘The student could not be saved. Please, try again.’));
}
$this->set(compact(‘student’));
}
I am using cakephp 4.
I would really appreciate if anyone could help me.
I will be interested in a definitive answer because I sat down to the same problem in 3.8 this morning. Patching the entity with its data and the associated records then saving did not result in the creation of the associated records.
However, if I patch the entity then add the associated data in a separate statement:
$member->shares = $shared
where $member is my entity and $shared is my array of associated data, all the records saved properly.
The specific failure seems to be that the associated data does not patch onto the entity; the shares property has an empty array after the patch.
Accounting for mass-assignment of that property has not solved the problem.
Are you seeing this same thing in your entity @siju ?