I have 3 Tables:
- ingredients (id, name)
- dishsizes (id, name)
- dishsizes_ingredients (id, dishsize_id, ingredient_id, unitprice)
I want to add a new Ingredient and define in the same form the unitprices.
in the add-form I have:
<?php foreach ($dishsizes as $i => $dishsize): ?><div class="flex flex-col w-full">
<label class="text-black"><?= $dishsize->name?></label>
<?= $this->Form->number('dishsizes.'.$i.'._joinData.unitprice',['min'=>1,'step'=>0.01,'class'=>'w-2/3 mt-2 mb-6 px-4 py-2 border rounded-lg text-lg text-gray-700 focus:outline-none','label'=>'none','hiddenField'=>false,'default'=>NULL])?>
<?= $this->Form->hidden('dishsizes.'.$i.'._joinData.currency_id',['value'=>1])?>
<?= $this->Form->hidden('dishsizes.'.$i.'._joinData.dishsize_id',['value'=>$dishsize->id])?>
</div>
<?php endforeach; ?>
This results in post data:
‘dishsizes’ => [
(int) 0 => [
‘_joinData’ => [
‘unitprice’ => ‘5’, ‘currency_id’ => ‘1’, ‘dishsize_id’ => ‘1’,
],
], (int) 1 => , (int) 2 => , (int) 3 => ,
So far so good, but the ingredient entity, created with
$ingredient = $this->Ingredients->patchEntity($ingredient, $this->request->getData(),['associated'=>['Foodadditives','Ingredientnotes','Dishsizes._joinData']]);
has errors in dishsizes.
'dishsizes' => [
(int) 0 => object(App\Model\Entity\Dishsize) id:13 {
'_joinData' => object(App\Model\Entity\DishsizesIngredient) id:14 { } '[new]' => true '[accessible]' => [ ] '[dirty]' => [ ] '[original]' => [ ] '[virtual]' => [ ] '[hasErrors]' => true '[errors]' => [
'name' => [
'_required' => 'This field is required',
],
]
I think the ingredient id is missing in the dishsizes part of the ingredient id.
But how to solve this?