如何在cakephp 3中保存多个记录 [英] How to save multiple records in cakephp 3

查看:91
本文介绍了如何在cakephp 3中保存多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cakephp 3的新手。我想使用多个复选框保存多个记录。
我在活动表&中有一些活动密码表中的一些密码。
我想在每个事件下设置不同的密码。
例如-对于事件1,我想设置一些密码,该密码将存储在event_password_all表中。

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)

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

我该怎么做。

这是我的控制器代码-

    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']);
}

&这是视图-

<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() ?>

推荐答案

3.2.8之前:

CakePHP 3中没有诸如saveAll之类的事件。
您必须遍历所有选项。

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

示例:

$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

为了使ppl步入正轨。

从3.2.8开始,一种更有效地做到这一点的方法。

食谱:保存多个实体

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

这篇关于如何在cakephp 3中保存多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆