【发布时间】:2020-03-25 17:06:22
【问题描述】:
我使用的是 Yii2 基础版。我有一个名为 groupsavingdetails 的表,用于保存每月的团体储蓄。
我有来自 2017 年和 8 月的 ID 为 29 的组的记录。同样,我有这个特定组从 2017 年 9 月、10 月、11 月和 12 月的记录。如图所示,该组还有 2018 年和 1 月和 3 月的数据。
过程是 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,]);
}
}
【问题讨论】:
-
因为 "$groups" 是一个数组。不是模型的对象
-
那我应该改变什么。
-
你的型号是什么
-
$model = new Groupsavingdetails();
标签: php mysql yii yii2 yii2-basic-app