# Saving Associated Models in CakePHP4

**URL:** <https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399>\
**Category:** Need Help\
**Created:** [May 17, 2022, 11:10am UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399 "2022-05-17T11:10:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [May 17, 2022, 11:10am UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399/1 "2022-05-17T11:10:39Z")

</div>

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.

```auto
            $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

```auto
$member->member_financials[0]->date_of_payment = $member->date_of_joining;

```

and very similar to what we did in CakePHP2

```auto
$this->request->data['MemberFinancial'][0]['date_of_payment'] = $dateOfJoining;

```

In the template I have the following;

hasMany

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

```

hasOne

```auto
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

```auto
'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.

```auto
'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

```auto
$this->hasOne('MemberStats', [
    'foreignKey' => 'member_id'
]);

```

and the MemberStats Table has

```auto
$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 ☹

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 17, 2022, 1:04pm UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399/2 "2022-05-17T13:04:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [May 17, 2022, 2:08pm UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399/3 "2022-05-17T14:08:24Z")

</div>

> [@Zuluru](#):
>
> 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

```auto
$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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 17, 2022, 3:03pm UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399/4 "2022-05-17T15:03:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [May 17, 2022, 8:45pm UTC](https://discourse.cakephp.org/t/saving-associated-models-in-cakephp4/10399/5 "2022-05-17T20:45:43Z")

</div>

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!
