Saving Associated Models in CakePHP4

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' => [

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;


echo $this->Form->control('member_financials.0.authority_card_number', [
    'label' => ['text' => 'Financial Receipt No.']


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 :frowning:

A hasOne relation will name the property member_stat, not member_stats, because there is only one of it. So that’s how you need to name your input field.

I’m just as confused…

The table is “member_stats” and the relationship is

$this->hasOne('MemberStats', [
   'foreignKey' => 'member_id'

When I change the property to ‘member_stat’ in the input field, now when I save there is no associated data in the Member model

Do I change the Associated name to MemberStat? Then I get table ‘member_stat’ does not exist. I’ve set className and Property - but still not getting anywhere.

Your association is set up correctly. The input field should be named member_stat.assist_with_transport instead of member_stats.assist_with_transport. If you tried it as just member_stat, I apologize for being unclear.

Ok - the final piece of the puzzle was that I hadn’t change the accessible field from member_stats to member_stat

All working at last - thanks for your assistance here!