Saving HasMany Association Data in CakePHP 3.x

I have two tables. My main table is students. And my secondary table is exams. I am trying to save both tables using hasMany and attribToMany . But it only stores data in the Student table, not in exams. Can anyone help me solve this problem.

Student Model:

class StudentsTable extends Table { public function initialize(array $config) { $this->addBehavior('Timestamp'); parent::initialize($config); $this->table('students'); $this->primaryKey(['id']); $this->hasMany('Exams', [ 'className' => 'Exams', 'foreignKey' => 'student_id', 'dependent'=>'true', 'cascadeCallbacks'=>'true']); } } 

Exam Model:

 class ExamsTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->table('exams'); $this->primaryKey(['id']); $this->belongsToMany('Students',[ 'className'=>'Students', 'foreignKey' => 'subject_id', 'dependent'=>'true', 'cascadeCallbacks'=>'true']); } } 

My school .ctp:

 echo $this->Form->create(); echo $this->Form->input('name'); echo $this->Form->input('exams.subject', array( 'required'=>false, 'multiple' => 'checkbox', 'options' => array( 0 => 'Tamil', 1 => 'English', 2 => 'Maths'))); echo $this->Form->button(__('Save')); echo $this->Form->end(); 

In my controller:

 public function school() { $this->loadModel('Students'); $this->loadModel('Exams'); $student = $this->Students->newEntity(); if ($this->request->is('post')) { $this->request->data['exams']['subject'] = implode(',',$this->request->data['exams']['subject']); $student = $this->Students->patchEntity( $student, $this->request->data, ['associated' => ['Exams']] ); if ($this->Students->save($student)) { $this->Flash->success(__('The user has been saved.')); } else { $this->Flash->error(__('Unable to add the user.')); } } } 
+5
source share
2 answers

Patching BelongsToMany Associations

You need to make sure that you can set exams. Set the available fields so that you can correct the related data.

 $student = $this->Students->patchEntity( $student, $this->request->data, [ 'associated' => ['Exams'], 'accessibleFields' => ['exams' => true] ] ); 

You can also do this using the $ _accessible property in the entity.

0
source

I never did hasMany to belong to many, because I don’t think it works that way (I mean no harm in my words.) But I will try to explain. Your relationship should belong to many at the same time, because there will be many students in the exams, and students will have many exams. So basically they are the same anyway. You will need another table to connect to them, which will be called students_exams or exams_students (I think its exams_students , because E comes before S), because if you name everything correctly, most of them happen automatically.

Assuming that you know how patchEntity works, creating your $this->request->data will correct correctly automatically and save it in the correct table when you save it. If you have more questions, feel free to ask more. :)

0
source

Source: https://habr.com/ru/post/1239981/


All Articles