public static function recalculateSummuBallov($id) { $bally = StrukturaOtsenochnogoLista::find()->where(['roditel' => $id])->sum('bally'); if (!$bally) { $bally = 1; } $roditel = StrukturaOtsenochnogoLista::findOne($id); $roditel->bally = $bally; if ($roditel->save()) { return true; } else { return false; } }
public function actionEditStruktura() { Yii::$app->response->format = Response::FORMAT_JSON; $response = new JsResponse(); $id = Yii::$app->request->post('id'); $nazvanie = Yii::$app->request->post('nazvanie'); if (!$nazvanie) { $response->type = JsResponse::ERROR; $response->msg = 'Введите название'; } $bally = Yii::$app->request->post('bally'); if (!$bally or $bally < 1) { $response->type = JsResponse::ERROR; $response->msg = 'Введите количество баллов, целое положительно число'; } if ($response->type != JsResponse::ERROR) { $editingItem = StrukturaOtsenochnogoLista::find()->where(['id' => $id])->one(); $editingItem->nazvanie = $nazvanie; $editingItem->bally = $bally; $transaction = Yii::$app->db->beginTransaction(); try { $editingItem->save(); $id = -1; if ($editingItem->roditel) { StrukturaOtsenochnogoLista::recalculateSummuBallov($editingItem->roditel); $id = $editingItem->roditel; } else { $id = $editingItem->id; } $transaction->commit(); $response->data = StrukturaOtsenochnogoLista::find()->joinWith('podstrukturaRel')->where(['struktura_otsenochnogo_lista.id' => $id])->orderBy(['struktura_otsenochnogo_lista.nomer' => SORT_ASC, 'podstruktura.nomer' => SORT_ASC])->asArray()->one(); } catch (\Exception $e) { $transaction->rollBack(); $response->type = JsResponse::ERROR; $response->msg = JsResponse::MSG_OPERATION_ERROR; throw $e; } } return $response; }