2017-01-24 49 views
0

我正在使用cakephp 2.0框架。我想在同一時間在這裏保存多行是我的數據如何在cakephp2.0中一次保存多行?

enter code here 
[EveAppointmentSlot] => Array 
    (
     [appointment_from_time] => Array 
      (
       [0] => 3:30 PM 
       [1] => 3:30 PM 
      ) 

     [appointment_to_time] => Array 
      (
       [0] => 3:45 PM 
       [1] => 3:45 PM 
      ) 

    ) 

) 

appointment_from_time零指數爲下午3:30和appointment_to_time指數爲零: - 下午3:45,並同其他指數 我想將數據保存在此表 enter image description here

現在,當保存像

enter code here 
appointment_from_ time  appointment_to_time 
3:30 PM       3:45 PM 
3:30 PM       3:45 PM 

在這裏,我已經使用這個代碼,但不能正常工作時,我想數據: -

enter code here 
foreach($data['EveAppointmentSlot'] as $k=> $v){ 
       echo $k; 
       foreach($data['EveAppointmentSlot'][$k] as $key=>$value){ 
        $this->EveAppointmentSlot->create(); 
        $this->EveAppointmentSlot->save($value); 
        } 
     } echo "sucess"; die; 
+0

你是什麼意思「*不能正常工作*」?你有錯誤嗎?錯誤的結果?沒有結果 ? –

+0

首先關閉所有數據來自哪裏?如果來自任何形式的帖子,那麼形式也是如此。然後事情會變得簡單。 –

回答

0
  1. 你的結構應該是

    [EveAppointmentSlot] => Array 
        (
         [0] => Array 
          (
           [appointment_from_time] => 3:30PM, 
           [appointment_to_time] => 3:45PM 
          ) 
         [1] => Array 
          (
           [appointment_from_time] => 3:30PM, 
           [appointment_to_time] => 3:45PM 
          ) 
        ) 
    
  2. 此外,數據應轉換爲日期數據。

所以從這些數據,您已經有了,我們在$data說,這可能幫助:

$new_data = array(); 
    foreach($data['EveAppointmentSlot'] as $attr => $slot) { 
     foreach($slot as $i => $value) { 
      $new_data[$i][$attr] = date('H:i:s', strtotime($value)); 
     } 
    } 

    if($this->EveAppointmentSlot->saveMany($new_data)) { 
     echo 'success'; 
    } 
    else { 
     echo 'failed'; 
    } 
0

您必須首先格式化數據以保存,然後使用saveMany一次保存它們。

$dataToSave = array(); 
foreach($data['EveAppointmentSlot'] as $k => $d){ 
    $index = 0; 
    foreach($d as $v){ 
     $dataToSave[$index][$k] = date("H:i:s", strtotime($v)); //Convert AM/PM time to 24h format supported by mysql. 
     $index++; 
    } 
} 
if($this->EveAppointmentSlot->saveMany($dataToSave)){ 
    echo "success"; 
}else{ 
    echo "error"; 
} 
+0

無法正常工作,出現錯誤情況 – kunal

+0

已更新,您能否告訴我您遇到的錯誤? –

0

你的結構應該是:

$new_data[EveAppointmentSlot] = Array 
(
    [0] => Array 
     (
      [appointment_from_time] => 3:30PM, 
      [appointment_to_time] => 3:45PM 
     ) 
    [1] => Array 
     (
      [appointment_from_time] => 3:30PM, 
      [appointment_to_time] => 3:45PM 
     ) 
) 

使用此格式,所有記錄保存在桌子裏。

if($this->EveAppointmentSlot->saveAll($new_data)) { 
    echo 'success'; 
} 
else { 
    echo 'failed'; 
}