I have an application in CakePHP 4 and am having problems saving associated model data. I have read Saving Associated Data in the Cake documentation but it's very unclear how this applies especially in my use-case.
The application has 3 tables which are relevant to this question:
items
sizes
items_sizes_wanted
The application allows users to request items of clothing (items
) and the form to input/save such an item has a dropdown of different sizes (sizes
). Each size has a unique ID. A user can select one or more size when saving an item. The items_sizes_wanted
table is supposed to hold one (or more) rows depending on the sizes the user selected, with the corresponding item ID. For example if they saved sizes 2, 3 and 4 for Item 999 there would be 3 rows in this table:
size_id | item_id
--------|---------
2 | 999
3 | 999
4 | 999
The code has been baked and the associations in the Table classes look ok:
// src/Model/Table/ItemsSizesWantedTable.php
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('items_sizes_wanted');
$this->belongsTo('Items', [
'foreignKey' => 'item_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Sizes', [
'foreignKey' => 'size_id',
'joinType' => 'INNER',
]);
}
The Entity class for the item also looks ok:
// src/Model/Entity/Item.php
// @property \App\Model\Entity\ItemsSizesWanted[] $items_sizes_wanted
protected $_accessible = [
// ...
'items_sizes_wanted' => true,
// ...
];
In the form where the item gets saved (src/templates/Items/add.php
) I have used the Form Helper and named it using dot notation:
<?php
// Note that $sizes is an array of key/value pairs from the
// 'sizes' table.
?>
<?= $this->Form->control('items_sizes_wanted.size_id', ['options' => $sizes, 'multiple' => 'multiple']) ?>
When rendered in the browser this produces a valid array syntax name. The <option>
's rendered inside all have valid ID's, i.e. the ones from the sizes
table.
<select name="items_sizes_wanted[size_id]" multiple="multiple">
When I save the data in my Controller (src/Controller/ItemsController.php
) using the following:
public function add()
{
$item = $this->Items->newEmptyEntity();
if ($this->request->is('post')) {
$item = $this->Items->patchEntity($item, $this->request->getData());
// Edit: some of the entity properties are manually set at this point, e.g.
$item->item_status = 'Wanted';
if ($this->Items->save($item)) {
$this->Flash->success(__('Your item has been listed.'));
}
}
}
The data is saved correctly to the items
table and the flash success message, "Your item has been listed." is displayed in the browser.
But - no data is written to items_sizes_wanted
.
I'm unsure why this is. The linked docs don't specifically say how the Form Helper should be used, so I'm assuming my syntax for that form field is correct, but it might not be.
If I debug the entity after pressing Save using debug($item); die;
in the Controller it has 'items_sizes_wanted' => [ ]
even though I selected multiple size options using the form.
Please can somebody help as I'm lost as to what's going wrong here?