Saving hasMany data and the PEBKAC error [resolved]

My associated data isn’t updating but i’m not sure why. Reasonably I’m forgetting to do something somewhere…

I have this in a pages controller:

$page = $this->Pages->patchEntity($page, $this->request->getData(), ['associated' => ['CustomFields']]);
error_log(print_r($page['custom_fields'],true));
if ($this->Pages->save($page)) {

which saves my page and logs this data:

Array
(
    [0] => App\\Model\\Entity\\CustomField Object
        (
            [id] => 1
            [key] => Call To Action
            [value] => Test Val123123
            [page_id] => 1
            [[new]] => 
            [[accessible]] => Array
                (
                    [key] => 1
                    [value] => 1
                    [page_id] => 1
                    [page] => 1
                )

            [[dirty]] => Array
                (
                    [value] => 1
                )

            [[original]] => Array
                (
                    [value] => Test Val
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[invalid]] => Array
                (
                )

            [[repository]] => CustomFields
        )
    )
)

But the Custom Field is not updated.

To drill down further my pages edit.ctp for the custom fields looks like:

    $i = 0;
    foreach ($page->custom_fields as $field) {
        echo $this->Form->hidden('custom_fields.' . $i . '.' . 'id');
        echo $this->Form->hidden('custom_fields.' . $i . '.' . 'key');
        echo $this->Form->Control('custom_fields.' . $i . '.' . 'value', ['label' => $field->key]);
        echo $this->Form->hidden('custom_fields.' . $i . '.' . 'page_id');
        $i++;
    }

The pagesTable hasMany:

    $this->hasMany('CustomFields', [
        'foreign_key' => 'page_id'
    ]);

The CustomFields belongsTo:

    $this->belongsTo('Pages', [
        'foreignKey' => 'page_id',
        'joinType' => 'INNER'
    ]);

CakePHP Version 3.6.5

whats debug output of $page after save block?

Apologies about the wait I had ripped the form out and done a little reworking to try saving as a “hasOne” which worked but now it as it was again.
$page after save:

App\\Model\\Entity\\Page Object
(
    [id] => 1
    [created] => 
    [modified] => Cake\\I18n\\FrozenTime Object
        (
            [time] => 2018-06-25T13:23:38+00:00
            [timezone] => UTC
            [fixedNowTime] => 
        )
        
    [title] => Design Test Page
    [content] => <h1>MAIN TITLE</h1><h2>Subtitle</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec fermentum libero, ut facilisis libero. Nam ornare vehicula quam non ornare. Etiam in dui libero. Vivamus sit amet ligula viverra magna tempus porta in eget nisl. Phasellus vitae arcu nec massa molestie varius. Mauris ut pretium elit. Vivamus purus ante, tincidunt et sagittis auctor, tempus vitae lectus. Proin sed sapien ex. Vestibulum id est non dolor condimentum faucibus posuere et sapien. Praesent risus massa, pharetra in ante suscipit, sodales vulputate sem. Aenean nec orci at augue cursus sodales mattis non erat. Ut elementum nibh risus, et commodo orci commodo ultrices. Nulla viverra bibendum sapien, id pharetra lectus sagittis vel. Proin lectus leo, tincidunt eget finibus nec, sagittis et nulla.</p>
    [excerpt] => home excerpt, test \r
    edit
    [custom_css] => 
    [custom_js] => 
    [custom_fields]=> Array
        (
            [0] => App\\Model\\Entity\\CustomField Object
                (
                    [id] => 1
                    [key] => Call To Action
      [value] => new value
                    [page_id] => 1
                    [field_type] => input
                    [[new]] => 
                    [[accessible]] => Array
                    (
                            [key] => 1
                            [value] => 1
                            [page_id] => 1
                            [field_type] => 1
                            [page] => 1
                        )
                        
                    [[dirty]] => Array
                        (
                            [value] => 1
                     )
                     
                    [[original]] => Array
                        (
                            [value] => Test Val1
                        )
                        
      [[virtual]] => Array
                        (
                        )
                        
                    [[errors]] => Array
                        (
                        )
                        
            [[invalid]] => Array
                        (
                        )
                        
                    [[repository]] => CustomFields
                )
                
            [1] => App\\Model\\Entity\\CustomField Object
                (
                    [id] => 2
                    [key] => Slider box 1
                    [value] => value for slider box 1 edit
                 [page_id] => 1
                    [field_type] => wysiwyg
                    [[new]] => 
                    [[accessible]] => Array
                        (
                      [key] => 1
                            [value] => 1
                            [page_id] => 1
                            [field_type] => 1
      [page] => 1
                        )
                        
                    [[dirty]] => Array
                        (
                        )
                        
                    [[original]] => Array
                      (
                        )
                        
                    [[virtual]] => Array
                        (
                        )
                        
                    [[errors]] => Array
                        (
                        )
                        
                    [[invalid]] => Array
                        (
                        )
                        
                    [[repository]] => CustomFields
                )
                
        )
        
    [[new]] => 
    [[accessible]] => Array
        (
            [created] => 1
            [modified] => 1
            [title]=> 1
            [content] => 1
            [excerpt] => 1
            [custom_css] => 1
            [custom_js] => 1
        )
        
    [[dirty]] => Array
        (
        )
        
    [[original]] => Array
        (
        )
        
    [[virtual]] => Array
        (
        )
        
    [[errors]] => Array
        (
        )
        
    [[invalid]] => Array
        (
 )
 
    [[repository]] => Pages
    )

Unsure if this is the RIGHT solution but for the moment it is working for me like this(the foreach being the bit what makes it work):

public function edit($id = null)
{
    $page = $this->Pages->get($id, [
        'contain' => ['CustomFields']
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $page = $this->Pages->patchEntity($page, $this->request->getData(), ['associated' => ['CustomFields']]);
        if ($this->Pages->save($page)) {
            $this->Flash->success(__('The page has been saved.'));
            $customFields = TableRegistry::get('CustomFields');
            foreach ($page->custom_fields as $custom_field) {
                $customFields->save($custom_field);
            }
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The page could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('page'));
}

you need to add custom_fields to accessible in Page.php