gpt4 book ai didi

php - Yii2:无法通过 + 1 更新列值

转载 作者:可可西里 更新时间:2023-11-01 07:49:31 25 4
gpt4 key购买 nike

我需要在创建新记录时通过 +1 更新列值:

public function actionCreate()
{
$model = new CreateBookings();
if ($model->load(Yii::$app->request->post())) {
Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type' ")->execute();
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

我做错了什么请指导:)

最佳答案

试试这个:

 Yii::$app->db->createCommand("UPDATE room_types SET total_booked=total_booked+1 WHERE room_type = '$model->room_type' ")->execute();

public function actionCreate()
{
$model = new CreateBookings();
if ($model->load(Yii::$app->request->post())) {

$RoomType = new room_types(); // room type replace with model name
$RoomType->updateCounters(['total_booked' => 1]);

$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

官方link

关于php - Yii2:无法通过 + 1 更新列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com