gpt4 book ai didi

php - 如何在 Yii 2 中更新大于所选记录的记录

转载 作者:行者123 更新时间:2023-11-29 15:25:27 26 4
gpt4 key购买 nike

我使用的是 Yii2 basic。我有一个名为 group savingdetails 的表,它可以保存每月的组节省。

我有 ID 为 29、2017 年 8 月的组记录。同样,我有 2017 年 9 月、10 月、11 月和 12 月该特定组的记录。如图所示,还有该组 2018 年以及 1 月和 3 月的数据。

enter image description here

该过程是,8 月份该组没有任何条目(即,这是第一次数据输入,因此期初余额设置为 0)。现在,对于 9 月,将 8 月的期末余额作为期初余额,依此类推。所以这些都是依赖的。

现在的情况是,当我想更改 9 月份的记录时,其期末余额将发生更改,因此这个新的期末余额将设置为 10 月份的期初余额,依此类推。 我应该怎么做?

下面是actionUpdate函数,它给我错误调用数组上的成员函数save()


public function actionUpdate($id) {
$model = $this->findModel($id);

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}

if ($model->load(Yii::$app->request->post())) {

$count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM groupsavingdetails WHERE ((groupsavingdetails.GroupId=:Id and Year>=:year and Month>:month) OR (GroupId=:Id and Year>:year AND Month>0))')->bindValues([':Id' => $model->GroupId, ':year' => $model->Year, ':month' => $model->Month]->queryAll();


if ($count == 0) {

}

if ($count == 1) {

}

if ($count > 1) {

if ($model->LoanGiven != 0) {
$model->TotalValueofLoanGiven += $model->LoanGiven;
}



if ($model->LoanRecovery != 0) {
$model->LoanRepaidUptilNow += $model->LoanRecovery;
}


$model->TotalValueOfLoanOutstanding = $model->TotalValueofLoanGiven - $model->LoanRepaidUptilNow;

$model->ClosingBalance = ($model->OpeningBalance + $model->TotalSaving + $model->LoanRecovery + $model->LoanInterest + $model->Fine + $model->BankInterest + $model->BankLoan - $model->Expenses - $model->LoanGiven);



$groups = Yii::$app->db->createCommand('SELECT * FROM groupsavingdetails WHERE ((groupsavingdetails.GroupId=:Id and Year>=:year and Month>:month) OR (GroupId=:Id and Year>:year AND Month>0))')->bindValues([':Id' => $model->GroupId, ':year' => $model->Year, ':month' => $model->Month])->queryAll();


foreach ($groups as $k => $group) {
if ($k == 0) {

$groups[$k]['OpeningBalance'] = $model->ClosingBalance;
$groups[$k]['TotalValueofLoanGiven'] = $model->TotalValueofLoanGiven;
$groups[$k]['LoanRepaidUptilNow'] = $model->LoanRepaidUptilNow;
$groups[$k]['TotalValueOfLoanOutstanding'] = $model->TotalValueOfLoanOutstanding;
}
}


$groups->save(); // Here it gives me error even if i write $groups[$k]->save()

$model->save();



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

最佳答案

您不能对数组调用save()(或任何其他函数)。函数是在对象上调用的。因此,您必须确保有一个对象,将数据加载到该对象上,然后保存该对象。你可以这样做:

foreach ($groups as $k => $group) {
if ($k === 0) {
$groupModel = new GroupSavingDetails(); // Or whatever the model is called..

// Load the data onto the model
$groupModel->OpeningBalance = $model->ClosingBalance;
$groupModel->TotalValueofLoanGiven = $model->TotalValueofLoanGiven;
$groupModel->LoanRepaidUptilNow = $model->LoanRepaidUptilNow;
$groupModel->TotalValueOfLoanOutstanding = $model->TotalValueOfLoanOutstanding;

// Save the model
$groupModel->save();
}
}

// Don't save $groups after the foreach, because that's still an array

这将(可能/希望)修复代码。但这可能仍然不是最好的方法。看来您也可以仅使用一个 UPDATE 查询来完成更新所有这些记录。我没有测试下面的代码,但这将创建一个更新查询,可以修改该查询以更新正确的行。这样您就不必检索所有单独的记录,逐一更新它们并为每一行运行更新命令。

Yii::$app->createCommand()->update(
'groupsavingdetails', // Or GroupSavingDetails::tableName()
[
'OpeningBalance' => $model->ClosingBalance;
'TotalValueofLoanGiven' => $model->TotalValueofLoanGiven,
'LoanRepaidUptilNow' => $model->LoanRepaidUptilNow,
'TotalValueOfLoanOutstanding' => $model->TotalValueOfLoanOutstanding,
],
[
'OR',
[
'AND',
['GroupId' => $model->groupId],
['>=', 'Year', $model->Year],
['>', 'Month', $model->Month],
],
[
'AND',
['GroupId' => $model->groupId],
['>', 'Year', $model->Year],
['>', 'Month', 0],
],
]
)->execute()

关于php - 如何在 Yii 2 中更新大于所选记录的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59115995/

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