[英]Save multiple records based on user input in Cakephp
What I'm using: cakephp version 2.4.1 我正在使用: cakephp版本2.4.1
What I have: 我有的:
table channel_settings with attributes (id, mask_id, provider_id, servicetype_id, channel_id, created, modified) 具有属性(id,mask_id,provider_id,servicetype_id,channel_id,已创建,已修改)的表channel_settings
table channel_alert_defaults with attributes (id, provider_id, servicetype_id, channel_id) 具有属性(id,provider_id,servicetype_id,channel_id)的表channel_alert_defaults
In page add new channel_setting user can insert each provider to each servicetype to it's channel. 在页面中添加新的channel_setting用户可以将每个提供程序插入其频道的每个服务类型。
Now what I need is when user choose servicetype to --Any-- then besides this one record, there will be some multiple insert into database for some servicetype because some servicetype need different channel. 现在我需要的是,当用户将服务类型选择为--Any--时,除了这一条记录外,对于某些服务类型还将在数据库中进行多次插入,因为某些服务类型需要不同的渠道。 Amount of multiple insert depent on how many a provider has servicetype setting and channel in table channel_alert_defaults
在表channel_alert_defaults中,有多少个提供程序具有服务类型设置和通道的多次插入插入量
Here's existing system: 这是现有系统:
Here's what I'm trying, but I still don't get any idea how multiple insert code is 这是我正在尝试的方法,但是我仍然不知道多个插入代码是多少
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: why I want this? PS:为什么我要这个? So that I don't have to insert data one by one for each provider.
这样我就不必为每个提供程序一个接一个地插入数据。 Thank you
谢谢
I can't test now but maybe you can try something like this: 我现在无法测试,但也许您可以尝试以下方法:
$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 thank you for give me inspiration on how I should solve the problem. @Javi谢谢您给了我解决问题的灵感。
What I've done: 1. count how many data from table channel_alert_defaults than correspondent to provider_id from user input 2. Make a loop insert into table channel_setting using saveAll 我做了什么:1.计算表channel_alert_defaults中的数据比用户输入中provider_id对应的数据多2.使用saveAll在表channel_setting中循环插入
Here is my code: 这是我的代码:
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.