I have a form that submit many Entities level at the same time, so prepare request Data array by HTML form is too much trouble.
The Entities associations is at below:
Users --> hasMany --> Pupils --> belongToMany --> Courses
I patch Users entity like this:
$user = $this->Users->newEntity($userData, [
'associated' => ['Pupils' => ['associated' => ['EduHistories', 'Courses']]]
]);
I posted request data like this:
But my entity after try to save is:
I am sure that my Users->Pupils->Courses can be saved because the previous code already did that, via approach of linking Pupils and Courses.
Since I added Payment_Method and Payment_Option into the join Table, I have change the code to patchEntity with associated fields, and the whole thing fail.
My PupilsTable has association as below:
$this->belongsToMany('Courses', [
'foreignKey' => 'pupil_id',
'targetForeignKey' => 'course_id',
'joinTable' => 'courses_pupils',
'through' => 'courses_pupils',
'saveStrategy' => 'append',
]);
And my Courses has association as below:
$this->belongsToMany('Pupils', [
'foreignKey' => 'course_id',
'targetForeignKey' => 'pupil_id',
'joinTable' => 'courses_pupils',
'through' => 'courses_pupils',
]);
My join table has associations as blow:
$this->belongsTo('Courses', [
'foreignKey' => 'course_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Pupils', [
'foreignKey' => 'pupil_id',
'joinType' => 'INNER',
]);
$this->belongsTo('PaymentOptions', [
'foreignKey' => 'payment_option_id',
]);
$this->belongsTo('PaymentMethods', [
'foreignKey' => 'payment_method_id',
]);
And of course all of my Entity class has associated as accessible.
Is there anyway that I can add _joinData into linking function Courses->links($courses, [$pupils]) ?
So that I can don’t need to patch long level entity.
Thank you for your help.