How to save multiple records in cakephp 3

荒凉一梦 提交于 2019-12-14 03:48:34

问题


I'm very new in cakephp 3. I want to save multiple records with multiple checkbox. I've some events in events table & some passwords in passwords table. I want to set different passwords under each events. For example- For event 1 I want to set some passwords which will be stored in event_password_all table.

(id, event1, password1), (id, event1, password2), (id, event1, password3), … … (id, event1, passwordN)

How can I do that.

Here is my controller code-

    public function add()
{
    $eventPasswordAll = $this->EventPasswordAll->newEntity();
    if ($this->request->is('post')) {       
        $eventPasswordAll = $this->EventPasswordAll->patchEntity($eventPasswordAll, $this->request->data);
        if ($this->EventPasswordAll->save($eventPasswordAll)) {
            $this->Flash->success(__('The event password all has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The event password all could not be saved. Please, try again.'));
        }
    }
    $events = $this->EventPasswordAll->Events->find('list', ['limit' => 200]);
    $passwords = $this->EventPasswordAll->Passwords->find('list', ['limit' => 200]);
    $this->set(compact('eventPasswordAll', 'events', 'passwords'));
    $this->set('_serialize', ['eventPasswordAll']);
}

& here is view-

<div class="eventPasswordAll form large-10 medium-9 columns">
<?= $this->Form->create($eventPasswordAll) ?>
<fieldset>
    <legend><?= __('Add Event Password All') ?></legend>
    <?php
        echo $this->Form->input('event_id', ['options' => $events]);
        echo $this->Form->input('password_id', ['options' => $passwords, 'multiple' => 'checkbox']);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>


回答1:


Before 3.2.8:
Theres no such event as saveAll in CakePHP 3. You must iterate through your options.

Example:

$passwords = $this->request->data('password_id');

foreach ($passwords as $password) {
    $data = [
        'event_id'    => $this->request->data('event_id'),
        'password_id' => $password
    ];
    $eventPasswordAll = $this->EventPasswordAll->newEntity();
    $this->EventPasswordAll->patchEntity($eventPasswordAll, $data);
    $this->EventPasswordAll->save($eventPasswordAll );
}

I did not test it, but you get the idea

>= 3.2.8
To keep ppl on the right track.
As of 3.2.8 there's a way to do this more efficient.
Cookbook: saving-multiple-entities

$data = [
    [
        'title' => 'First post',
        'published' => 1
    ],
    [
        'title' => 'Second post',
        'published' => 1
    ],
];
$articles = TableRegistry::get('Articles');
$entities = $articles->newEntities($data);
$result = $articles->saveMany($entities);



回答2:


From CakePHP version 3.2.8 you can save multiple records at once. You can use below code as mentioned on the cakephp documents.

https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-multiple-entities

$data = [
 [
    'title' => 'First post',
    'published' => 1
 ],
 [
    'title' => 'Second post',
    'published' => 1
 ],
];
$articles = TableRegistry::get('Articles');
$entities = $articles->newEntities($data);
$result = $articles->saveMany($entities);


来源:https://stackoverflow.com/questions/31691451/how-to-save-multiple-records-in-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!