我正在使用的内容: cakephp版本2.4.1
我有什么:
表 channel_settings ,包含属性(id,mask_id,provider_id,servicetype_id,channel_id,已创建,已修改)
表 channel_alert_defaults ,包含属性(id,provider_id,servicetype_id,channel_id)
在页面添加中,新的channel_setting用户可以将每个提供者插入每个服务类型到它的频道。
现在我需要的是当用户选择servicetype到--Any--然后除了这一条记录之外,对于某些servicetype,会有一些多次插入数据库,因为某些servicetype需要不同的通道。多个插入的数量取决于提供者在表中的服务类型设置和频道的数量 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:为什么我要这个?因此,我不必为每个提供商逐个插入数据。谢谢
答案 0 :(得分:0)
我现在无法测试,但也许你可以尝试这样的事情:
$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));
答案 1 :(得分:0)
@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);
}