我正在使用cakephp 2.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 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)
您必须首先格式化要保存的数据,然后使用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";
}
答案 1 :(得分:0)
您的结构应该是
[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
)
)
此外,数据应转换为日期数据。
因此,根据您已有的数据,让我们在$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';
}
答案 2 :(得分: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';
}