PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

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

 February 02, 2022     cakephp, cakephp-4.x     No comments   

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

  • Cookbook > Views > Helpers > Form > Field Naming Conventions
  • Cookbook > Database Access & ORM > Saving Data > Converting Request Data into Entities > Converting Multiple Records
  • Cookbook > Database Access & ORM > Saving Data > Saving Multiple Entities


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing