How to save multiple entries in cakephp 3
I am very new to cakephp 3. I want to keep multiple entries with multiple checkboxes. I have some events in the event table and some passwords in the password table. I want to set different passwords under each event. For example. For event 1, I want to set some passwords to be stored in the event_password_all table.
(id, event1, password1), (id, event1, password2), (id, event1, password3), ...... (id, event1, passwordN)
How can i do this.
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 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() ?>
source to share
Deprecated: (see below for solution)
CakePHP 3 does not have an event like saveAll. You have to iterate over 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 haven't tested it, but you get the idea
Edit:
To keep the ppl on the right track.
Since 3.2.8 there is a way to make it 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);
source to share
From CakePHP version 3.2.8 you can save multiple entries at once. You can use below code as mentioned in cakephp docs.
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);
source to share