0
我想插入很多記錄到數據庫中一個一個動作。用yii2插入多個記錄到數據庫
在這個控制器我用的foreach用於插入到數據庫中,但只是最後一條記錄插入到數據庫中,我不知道爲什麼。我想插入全部記錄到數據庫。
我的控制器:
if (isset($_POST['month'])) {
$name = $_POST['month'];
$price = $_POST['Request'];
$i = 0;
foreach ($name as $month) {
$model->month = $month;
$model->price = $price['price'];
$model->save(false);
$i++;
}
$pay_info = [
'cost' => $price['price'],
'title' => 'title'];
return $this->render('payment', ['pay_info' => $pay_info]);
}
謝謝它的作品。你能用batchinsert告訴我這個動作嗎? –
我已更新答案.. – scaisEdge
非常感謝。這很有用 –