如何使用cake php在表格中保存多个条目?这是我到目前为止编写的代码。
$err_counter = 1;
$data3 = array();
foreach ($data2['MembershipTime'] as $k => $values)
{
$batch_id = $lastid;
$day = $values['day'];
$skip = (!empty($values['skip'])) ? $values['skip'] : "";
$h1 = (!empty($values['h1'])) ? $values['h1'] : "00";
$m1 = (!empty($values['m1'])) ? $values['m1'] : "00";
$h2 = (!empty($values['h2'])) ? $values['h2'] : "00";
$m2 = (!empty($values['m2'])) ? $values['m2'] : "00";
$data3['BatchesTimes']['batch_id'] = $batch_id;
$data3['BatchesTimes']['day'] = $day;
$data3['BatchesTimes']['skip'] = $skip;
$data3['BatchesTimes']['h1'] = $h1;
$data3['BatchesTimes']['m1'] = $m1;
$data3['BatchesTimes']['h2'] = $h2;
$obj = $this->BatchesTimes->save($data3);
}
答案 0 :(得分:0)
这里有一个我自己的数组的示例示例,我将向您解释。您可以根据数组中的名称更改变量。
$this->ModelName->saveAll($data3);
如果上述解决方案不起作用,请尝试在解决的示例中提到的以下解决方案。
在你的情况下$ dataMulti = $ data3; 例如:
$dataMulti = array(
array(
'Info' =>
array(
'note' => "This is note 1",
'delete_flag' => "f"
)
),
array(
'Info' =>
array(
'note' => "This is note 2",
'delete_flag' => "f"
)
),
array(
'Info' =>
array(
'note' => "This is note 3",
'delete_flag' => "f"
)
),
);
一次保存很多记录我们有以下两种方式:
1. $infoModel->saveAll($dataMulti);
或
2. $infoModel->saveMany($dataMulti);
希望它有效。