如何更新大于选定的记录以在Yii 2中更新

问题描述 投票:0回答:1

我正在使用Yii2 basic。我有一个名为groupsavingdetails的表,该表可保存每月的组保存。

我有ID为29的组记录,从2017年到8月。同样,我也有2017年9月,10月,11月和12月该特定小组的记录。如图中所示,还有该组的2018年数据以及一月和三月的数据。

enter image description here

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

现在的情况是,当我想更改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,]); } }
php mysql yii yii2 yii2-basic-app
1个回答
0
投票
您要检索的日期作为模型对象的查询

$groups = Groupsavingdetails::find() ->andWhere(['and', ["GroupId" => $model->GroupId], ['>=', "Year", $model->Year], ['>', "Month", $model->Month], ]) ->orWhere(['and', ["GroupId" => $model->GroupId], ['>=', "Year", $model->Year], ['>', "Month", $model->Month], ])->all();

您可以像下面那样轻松保存此内容

foreach ($groups as $k => $group) { if ($k == 0) { $group->OpeningBalance = $model->ClosingBalance; $group->TotalValueofLoanGiven = $model->TotalValueofLoanGiven; $group->LoanRepaidUptilNow = $model->LoanRepaidUptilNow; $group->TotalValueOfLoanOutstanding = $model->TotalValueOfLoanOutstanding; $group->save(); } }

© www.soinside.com 2019 - 2024. All rights reserved.