Wednesday, February 2, 2022

[FIXED] How can I create input field for saveMany() in cakephp?

Issue

I have written name[] for insert multiple name using cakephp saveMany() method.

<?= $this->Form->control('name[]',['label'=>'Name']);?>

Problem is I'm getting the array like

[
  'name' => [
    (int) 0 => 'A',
    (int) 1 => 'B',
  ],
]

Getting error message Cake\ORM\Table::saveMany(): Argument #1 ($entities) must be of type iterable

How can I create name input field for multiple insert ?


Solution

Use the form helper's dot syntax with indices, that is 0.name, 1.name, 2.name, etc., this will result in data formatted like

[
    0 => [
        'name' => 'A',
    ],
    1 => [
        'name' => 'B',
    ],
    2 => [
        'name' => 'C',
    ],
    // ...
]

And then use Table::newEntities() / Table::patchEntities() to create/patch multiple entities at once.

See also



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.