I’m currently trying to save a model and several associated models, and I’m struggling with one that is as hasOne relationship. It works like in the old CakePHP2 app but not here.
$member = $this->Members->patchEntity($member, $this->request->getData(), [
'associated' => [
'MemberUnits',
'MemberStatuses',
'MemberFinancials',
'MemberStats'
]
]);
MemberStats is the hasOne relationship, the others are one to many
For the one to many, I’m updating some of the field with default - all good
$member->member_financials[0]->date_of_payment = $member->date_of_joining;
and very similar to what we did in CakePHP2
$this->request->data['MemberFinancial'][0]['date_of_payment'] = $dateOfJoining;
In the template I have the following;
hasMany
echo $this->Form->control('member_financials.0.authority_card_number', [
'label' => ['text' => 'Financial Receipt No.']
]);
hasOne
echo $this->Form->control('member_stats.assist_with_transport', [
'type' => 'checkbox',
'label' => 'Assist with transportation to camps and outings?']);
What I’m finding is the hasOne relationship isn’t being saved - no errors etc
I stuck in a debug to see what was in member - the hasMany are all proper objects eg
'member_financials' => [
(int) 0 => object(App\Model\Entity\MemberFinancial) id:5 {
'date_of_expiry' => object(Cake\I18n\FrozenDate) id:6 { }
'authority_card_number' => '123123123'
'joining_fee' => true
'date_of_payment' => object(Cake\I18n\FrozenDate) id: 2 {}
'amount' => (float) 75
But the hasOne record is just an array.
'member_stats' => [
'assist_with_transport' => '0',
'assist_branch_committee' => '0',
'assist_officer' => '0',
'teach' => '1',
Is there something different with the hasOne relationship?
The Members Table has the following relationship
$this->hasOne('MemberStats', [
'foreignKey' => 'member_id'
]);
and the MemberStats Table has
$this->belongsTo('Members', [
'foreignKey' => 'member_id',
]);
This is making me feel dumb - it wasn’t a hassle in v2 but I’ve been scratching my head at what’s wrong here