cakephp2.0如何一次保存多行?
How to save multiple rows at one time in cakephp2.0?
我正在使用 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:30pm 且 appointment_to_time 索引为零:- 3:45pm 与其他索引相同
我想在此 table 中保存数据
现在当我想要数据时像
一样保存
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;
需要先格式化数据保存,然后使用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";
}
你的结构应该是
[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';
}
你的结构应该是:
$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
)
)
使用此格式,所有记录都保存在 table。
if( $this->EveAppointmentSlot->saveAll( $new_data ) ) {
echo 'success';
}
else {
echo 'failed';
}
我正在使用 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:30pm 且 appointment_to_time 索引为零:- 3:45pm 与其他索引相同
我想在此 table 中保存数据
现在当我想要数据时像
一样保存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;
需要先格式化数据保存,然后使用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";
}
你的结构应该是
[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';
}
你的结构应该是:
$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
)
)
使用此格式,所有记录都保存在 table。
if( $this->EveAppointmentSlot->saveAll( $new_data ) ) {
echo 'success';
}
else {
echo 'failed';
}