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

hiz5n14c  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(206)

我正在使用Yii2 basic。我有一个名为groupsavingdetails的表,它保存每月的组保存。
我有ID为29的组的2017年8月的记录。同样,我有这个特定组的2017年9月、10月、11月和12月的记录。还有这个组的2018年1月和3月的数据,如图所示。

其过程是:8月份本集团没有录入(即第一次录入数据,期初余额设置为0),现在9月份取8月份得期末余额作为期初余额,以此类推,所以这些是有依赖关系得。

**现在的情况是,当我想更改9月份的记录时,其期末余额将被更改,因此新的期末余额将被设置为10月份的期初余额,依此类推。**我应该如何操作?

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

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,]);
        }
    }
ljsrvy3e

ljsrvy3e1#

你不能在数组上调用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()

相关问题