gpt4 book ai didi

php - 根据 Cakephp 中的用户输入保存多条记录

转载 作者:行者123 更新时间:2023-11-29 03:29:44 24 4
gpt4 key购买 nike

我正在使用什么:cakephp 版本 2.4.1

我有什么:

具有属性(id、mask_id、provider_id、servicetype_id、channel_id、创建、修改)的表channel_settings

具有属性(id、provider_id、servicetype_id、channel_id)的channel_alert_defaults

在页面中添加新的 channel_setting 用户可以将每个提供商插入每个服务类型到它的 channel 。

现在我需要的是,当用户将服务类型选择为 --Any-- 时,除了这条记录之外,还会有一些针对某些服务类型多次插入数据库,因为某些服务类型需要不同的 channel 。多次插入的数量取决于提供者在表 channel_alert_defaults

中有多少服务类型设置和 channel

这是现有系统: existing condition

我现在想要的: enter image description here

这是我正在尝试的,但我仍然不知道多重插入代码是怎样的

    public function add() {
if ($this->request->is('post')) {
Controller::loadModel('Servicetype');
$this->Servicetype->recursive = -1;
$servicetype = $this->Servicetype->find('all');
$this->request->data['ChannelSetting']['mask_id'] = $this->Session->read('current_mask_id');
$maskid = $this->request->data['ChannelSetting']['mask_id'];
$providerid = $this->request->data['ChannelSetting']['provider_id'];
$servicetypeid = $this->request->data['ChannelSetting']['servicetype_id'];

$this->ChannelSetting->create();

if ($this->request->data['ChannelSetting']['servicetype_id'] == 0) {

Controller::loadModel('ChannelAlertDefaults');
$this->ChannelAlertDefault->recursive = -1;
$channelalertdefault = $this->ChannelAlertDefaults->findByProviderId($providerid);
// loop insert goes here, I think...

if ($this->ChannelSetting->save($this->request->data)) {
$this->Session->setFlash(__('The channel setting has been saved'), 'success');
return $this->redirect(array('action' => 'add'));
}
else {
$this->Session->setFlash(__('The channel setting failed to save'));
}

} else {
if ($this->ChannelSetting->save($this->request->data)) {
$this->Session->setFlash(__('The channel setting has been saved'), 'success');
return $this->redirect(array('action' => 'add'));
}
else {
$this->Session->setFlash(__('The channel setting failed to save'));
}
}

if ($this->ChannelSetting->save($this->request->data)) {
$this->Session->setFlash(__('The channel setting has been saved'), 'success');
return $this->redirect(array('action' => 'add'));
}
else {
$this->Session->setFlash(__('The channel setting failed to save'));
}
}

PS:我为什么要这个?这样我就不必为每个提供者一个一个地插入数据。谢谢

最佳答案

我现在无法测试,但也许你可以尝试这样的事情:

$data = array(
[0] => array(
'MASK' => $this->request->data['ChannelSetting']['mask_id'],
'PROVIDER' => $this->request->data['ChannelSetting']['provider_id'],
'SERVICE_TYPE' => *all*
),
[1] => array(
'MASK' => $this->request->data['ChannelSetting']['mask_id'],
'PROVIDER' => $this->request->data['ChannelSetting']['provider_id'],
'SERVICE_TYPE' => *otp*
),
[2] => array(
'MASK' => $this->request->data['ChannelSetting']['mask_id'],
'PROVIDER' => $this->request->data['ChannelSetting']['provider_id'],
'SERVICE_TYPE' => *httpalert*
)
);

$this->ChannelSetting->saveAll($data, array('deep' => true));

关于php - 根据 Cakephp 中的用户输入保存多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com