我有一个表单,用户填写“姓名”,“时间点”,“位置”,“电话”,“备注”。有时,多个人同时报告同一台站。不是重复工作并在多个表单完成中提交相同的信息,而是使用多个名称(或者在这种情况下为资源编号###)分隔名称字段。我有来自控制器中表单的数据,可以接受一个名称或检测多个名称 - 由*分隔。问题是当代码看起来保存多个名称时,只有在完成所有操作后才能在表格中找到最后一条记录。
default flash messages showing records saved.
来自StationEntriesController的操作符函数执行上述操作。
public function operator() {
// Add then list
// Add information
$stationEntry = $this->StationEntries->newEntity();
if ($this->request->is('post')) {
// Determine is this is mulitple entries from a single entry with '*'
$data=$this->request->getData(); // Get form returned Data
$namecount=0; // $namecount holds number of names held in single name record
if (isset($data['name'])) { // It is posted data and name is filled in
$name=$data['name']; // Pull 'name' out of array
if(strpos($name, '*')){ // Check 'name' for * indicating multiple names need handling
unset($newdata);
do{
$firstPos=strpos($name, '*'); // Find first * position in 'name'
$nameToDelimeter=substr($name, 0,$firstPos); // create string from beginning to * substring of 'name'
$nameToDelimeter=chop($nameToDelimeter," "); // Remove any ' ' from the string
if(strlen($nameToDelimeter)==3){ // len will always be 3 if user applied correctly. (error hanlding to follow)
// $newdata is now array of form posted data with 1 of n 'name's and all other data remains the same.
$newdata[]=['location'=>$data['location'],'name'=>$nameToDelimeter, 'time_in'=>$data['time_in'],'phone'=>$data['phone'],'remarks'=>$data['remarks']];
$namecount++; // increment name count
}
$name=substr($name, $firstPos+1); // trim $name from beginning to * and leave the rest
}while(strpos($name, '*'));
// all delimters are out of $name now, so should be just one name left, let's add it to array copying other data
$name=chop($name," ");
$newdata[]=['location'=>$data['location'],'name'=>$name, 'time_in'=>$data['time_in'],'phone'=>$data['phone'],'remarks'=>$data['remarks']];
$namecount++;
$data=$newdata;
// End of form data manipulation
}
}
if($namecount){
// cycle through $data array and update database with the mutliple records
for($datacount=0;$datacount<$namecount;$datacount++){
$stationEntry = $this->StationEntries->patchEntity($stationEntry, $data[$datacount]); //add one row at a time
if ($this->StationEntries->save($stationEntry)) {
$this->Flash->default(__($data[$datacount]['name'].' has been logged on station.' ));
} else {
$this->Flash->error(__('The station entry could not be saved. Please, try again.'));
}
}
$this->redirect(['action' => 'operator']);
} else {
$stationEntry = $this->StationEntries->patchEntity($stationEntry, $this->request->getData());
if ($this->StationEntries->save($stationEntry)) {
$this->Flash->success(__('The station entry has been saved.'));
return $this->redirect(['action' => 'operator']);
}
$this->Flash->error(__('The station entry could not be saved. Please, try again.'));
}
}
$users = $this->StationEntries->Users->find('list', ['limit' => 200]);
$this->set(compact('stationEntry', 'users'));
$this->set('_serialize', ['stationEntry']);
输入数据格式:
'location' = "AnyWhere"
'name' = "309*214*217"
'time_in' = *Current* datetime
'phone' = ""
'remarks' = "Applicable Notes"
返回以下数据:
[
'location' => 'AnyWhere',
'name' => '309*214*217',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
]
$ data []中的操作表单数据现在显示:
[
(int) 0 => [
'location' => 'AnyWhere',
'name' => '309',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
],
(int) 1 => [
'location' => 'AnyWhere',
'name' => '214',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
],
(int) 2 => [
'location' => 'AnyWhere',
'name' => '217',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
]
]
这应该通过
一次向数据库添加一条记录$stationEntry = $this->StationEntries->patchEntity($stationEntry, $data[$datacount]);
$this->StationEntries->save($stationEntry)
只保存'217'记录 - 每个字段都是正确的,只有一个而不是三个条目。 “309”和“214”的条目不在表中。我相信这种方法不稳定,我无法复制,它确实创建了三个条目,但都名为217。
我试图保持在cakephp及其数据安全方法的范围内。这是我学习cakephp的“深入跳跃”方式,所以试图避免手动sql INSERT语句。
INSERT INTO `station_entries`(`location`, `name`, `time_in`, `time_out`, `phone`, `remarks`)
VALUES
('Anywhere', '309', '3017-08-09 19:28','','Applicable Notes'),
('Anywhere', '214', '3017-08-09 19:28','','Applicable Notes'),
('Anywhere', '217', '3017-08-09 19:28','','Applicable Notes');
感谢任何帮助和/或反馈。有没有办法通过我不熟悉的cakephp语句添加3条记录,这是什么?
谢谢。
答案 0 :(得分:0)
试试这个:
$data =[
(int) 0 => [
'location' => 'AnyWhere',
'name' => '309',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
],
(int) 1 => [
'location' => 'AnyWhere',
'name' => '214',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
],
(int) 2 => [
'location' => 'AnyWhere',
'name' => '217',
'time_in' => [
'year' => '2017',
'month' => '08',
'day' => '09',
'hour' => '19',
'minute' => '28'
],
'phone' => '',
'remarks' => 'Applicable Notes'
]
];
foreach ($data as $key => $d) {
$stationEntry = $this->StationEntries->newEntity();
$stationEntry = $this->StationEntries->patchEntity($stationEntry, $d);
$this->StationEntries->save($stationEntry);
}