public function actionRemoveImport($id) { if (Yii::$app->request->isAjax) { $transaction = Yii::$app->db->beginTransaction(); try { echo 'Traflog deleted ' . Traflog::deleteAll(['id_logreport' => $id]) . ' rows.<br>'; echo 'Matlog deleted ' . Matlog::deleteAll(['id_logreport' => $id]) . ' rows.<br>'; echo 'Employeelog deleted ' . Employeelog::deleteAll(['id_logreport' => $id]) . ' rows.<br>'; echo 'Logreport deleted ' . Logreport::deleteAll(['logreport_id' => $id]) . ' rows.<br>'; $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); throw new Exception($e->getMessage()); } } }
/** * @return \yii\db\ActiveQuery */ public function getTraflogs() { return $this->hasMany(Traflog::className(), ['id_matlog' => 'matlog_id'])->from(['traflogs' => Traflog::tableName()]); }
/** * Выполняем удаление отчетов, если превышен лимит количества хранящихся отчетов. * @throws Exception */ public function Execute() { foreach ($this->getNeedDeleteReports() as $Row) { $Transaction = Yii::$app->db->beginTransaction(); try { Traflog::deleteAll(['id_logreport' => $Row['logreport_id']]); Matlog::deleteAll(['id_logreport' => $Row['logreport_id']]); Employeelog::deleteAll(['id_logreport' => $Row['logreport_id']]); Logreport::findOne($Row['logreport_id'])->delete(); $FileRoot = 'importreports/Отчет импорта в систему Фрегат N' . $Row['logreport_id'] . '.xlsx'; if (DIRECTORY_SEPARATOR !== '/') { $FileRoot = mb_convert_encoding($FileRoot, 'Windows-1251', 'UTF-8'); } unlink($FileRoot); $Transaction->commit(); } catch (Exception $e) { $Transaction->rollBack(); throw new Exception($e->getMessage() . ' logreport_id = ' . $Row['logreport_id']); } } }
private static function DeleteOldReports() { $countreports = Importconfig::findOne(1); if (!empty($countreports)) { $files = glob('importreports/*.xlsx'); if (count($files) > $countreports->logreport_reportcount) { $ToDelete = Logreport::find()->select(['logreport_id'])->orderBy(['logreport_id' => SORT_ASC])->limit(count($files) - $countreports->logreport_reportcount)->asArray()->all(); } if (!empty($ToDelete)) { foreach ($ToDelete as $row) { Traflog::deleteAll(['id_logreport' => $row['logreport_id']]); Matlog::deleteAll(['id_logreport' => $row['logreport_id']]); Employeelog::deleteAll(['id_logreport' => $row['logreport_id']]); Logreport::findOne($row['logreport_id'])->delete(); $fileroot = 'importreports/Отчет импорта в систему Фрегат N' . $row['logreport_id'] . '.xlsx'; if (DIRECTORY_SEPARATOR !== '/') { $fileroot = mb_convert_encoding($fileroot, 'Windows-1251', 'UTF-8'); } unlink($fileroot); } } } }