[英]Save multiple records based on user input in Cakephp
我正在使用: 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用户可以将每个提供程序插入其频道的每个服务类型。
现在我需要的是,当用户将服务类型选择为--Any--时,除了这一条记录外,对于某些服务类型还将在数据库中进行多次插入,因为某些服务类型需要不同的渠道。 在表channel_alert_defaults中,有多少个提供程序具有服务类型设置和通道的多次插入插入量
这是我正在尝试的方法,但是我仍然不知道多个插入代码是多少
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));
@Javi谢谢您给了我解决问题的灵感。
我做了什么:1.计算表channel_alert_defaults中的数据比用户输入中provider_id对应的数据多2.使用saveAll在表channel_setting中循环插入
这是我的代码:
Controller::loadModel('ChannelAlertDefault');
$this->ChannelAlertDefault->recursive = 1;
$channelalertdefault = $this->ChannelAlertDefault->findAllByProviderId($providerid);
$amount = count($channelalertdefault); // to count how many array
// Here is the loop...
if ($this->ChannelSetting->save($this->request->data)) {
for($i=0;$i<$amount;$i++) {
$this->request->data['ChannelSetting']['mask_id'] = $this->Session->read('current_mask_id');
$this->request->data['ChannelSetting']['provider_id'] = $channelalertdefault[$i]['ChannelAlertDefault']['provider_id'];
$this->request->data['ChannelSetting']['servicetype_id'] = $channelalertdefault[$i]['ChannelAlertDefault']['servicetype_id'];
$this->request->data['ChannelSetting']['channel_id'] = $channelalertdefault[$i]['ChannelAlertDefault']['channel_id'];
$this->ChannelSetting->saveAll($this->request->data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.