我用的是:
cakephp版本2.4.1
我所拥有的:
带属性的表channel_设置(id、mask_id、provider_id、servicetype_id、channel_id、created、modified)
表channel_alert_缺省值,带有属性(id、provider_id、servicetype_id、channel_id)
在“添加新频道”设置页中,用户可以将每个提供程序插入到每个服务类型的频道中。
现在我需要的是,当用户选择servicetype为--Any--时,除了这一条记录之外,还会有一些servicetype的多次插入到数据库中,因为某些servicetype需要不同的通道。多个插入的数量取决于提供程序在表channel_alert_defaults中具有servicetype设置和频道的数量
现有的系统:
我现在想要的是:
这是我正在尝试的,但是我仍然不知道多个插入代码是什么
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'));
}
}
}
我为什么要这个?这样我就不必为每个提供者逐个插入数据。谢谢你
最佳答案
我现在不能测试,但也许你可以试试这样的东西:
$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));