Problem adding associative has many data in cakephp 4

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’ => [
if ($this->Studnets->save($student )) {
$this->Flash->success((‘The student data has been saved.’));
return $this->redirect([‘action’ => ‘index’]);
(‘The student could not be saved. Please, try again.’));

I am using cakephp 4.
I would really appreciate if anyone could help me.

Thank you

What are $this->Studnets and $this->Studnet? They should both be $this->Students? How do you not get errors from those lines?

Hi Zuluru, That’s just typo. If I did $this->Students also it doesn’t work.

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 ?

The problem is solved for me. The mistake was naming in the add field.

@dreamingmind, The real problem may in the naming convention in view (form field) like mine.

@siju, My problem was that the associated data was provided to patch as an array of entities rather than as an array of arrays.