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 actionDeleteStruktura() { Yii::$app->response->format = Response::FORMAT_JSON; $response = new JsResponse(); $id = Yii::$app->request->post('id'); $transaction = Yii::$app->db->beginTransaction(); try { $deletingItem = StrukturaOtsenochnogoLista::findOne($id); if (!is_null($deletingItem->roditel)) { StrukturaOtsenochnogoLista::updateAll(['nomer' => new Expression('nomer-1')], 'roditel=' . $deletingItem->roditel . ' and nomer>' . $deletingItem->nomer); } else { StrukturaOtsenochnogoLista::updateAll(['nomer' => new Expression('nomer-1')], 'otsenochnyj_list=' . $deletingItem->otsenochnyjList . ' and nomer>' . $deletingItem->nomer); StrukturaOtsenochnogoLista::deleteAll(['roditel' => $id]); } $roditel = $deletingItem->roditel; $deletingItem->delete(); if (!is_null($roditel)) { StrukturaOtsenochnogoLista::recalculateSummuBallov($roditel); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $response->type = JsResponse::ERROR; $response->msg = JsResponse::MSG_OPERATION_ERROR; throw $e; } if (!is_null($roditel)) { $response->data = StrukturaOtsenochnogoLista::find()->joinWith('podstrukturaRel')->where(['struktura_otsenochnogo_lista.id' => $roditel])->orderBy(['struktura_otsenochnogo_lista.nomer' => SORT_ASC, 'podstruktura.nomer' => SORT_ASC])->asArray()->one(); } return $response; }