diff --git a/README.md b/README.md index 88cf52c..e9ad335 100644 --- a/README.md +++ b/README.md @@ -343,13 +343,13 @@ D3filesModel::createCopy($fileModelId, Users::class, $model->id); ### Maintenance commands -Soft delete +Soft deletes files (sets deleted=0) ```bash -d3files/remove-older-than full/model/name yyyy-mm-dd +d3files/remove-older-than full/model/name months ``` -Deletes files and corresponding records in database +Deletes files and corresponding records in database which have deleted=0 ```bash d3files/remove-files full/model/name diff --git a/commands/CleanFilesController.php b/controllers/CleanFilesController.php similarity index 76% rename from commands/CleanFilesController.php rename to controllers/CleanFilesController.php index 5f62f2b..b1298b1 100644 --- a/commands/CleanFilesController.php +++ b/controllers/CleanFilesController.php @@ -1,32 +1,31 @@ innerJoin('d3files', '`d3files`.`id` = `d3files_model`.`d3files_id`') @@ -63,6 +62,8 @@ public function actionRemoveFiles($modelName) ->where(['deleted' => 1]) ->all(); + $this->stdout('Deleting ' . count($deletedFiles) . ' file models.'); + foreach ($deletedFiles as $fileModel) { $file = $fileModel->getD3files()->one(); @@ -78,14 +79,17 @@ public function actionRemoveFiles($modelName) $filePath = $fileHandler->getFilePath(); $fileModel->delete(); - if (!D3filesModel::findOne(['d3files_id' => $file->id])) { + if (!$usedModel = D3filesModel::findOne(['d3files_id' => $file->id])) { $file->delete(); if (file_exists($filePath)) { unlink($filePath); } + } else { + $this->stdout('Can\'t delete file ' . $file->file_name . ', in use with model: '. $usedModel->id); } + } return 0;