From b28600f59168c548370f8df2e7282e2b286c8f60 Mon Sep 17 00:00:00 2001 From: Christian Wach Date: Tue, 22 Mar 2022 16:18:41 +0000 Subject: [PATCH] Keep "EntityFile" records available until after Symfony "delete" hooks fire --- CRM/Core/BAO/File.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index 6f4401daf1..dc2e7ac026 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -265,6 +265,19 @@ class CRM_Core_BAO_File extends CRM_Core_DAO_File { $cefIDs[] = $dao->cefID; } + // Delete tags from entity tag table. + if (!empty($cfIDs)) { + $deleteFiles = []; + foreach ($cfIDs as $fId => $fUri) { + $tagParams = [ + 'entity_table' => 'civicrm_file', + 'entity_id' => $fId, + ]; + CRM_Core_BAO_EntityTag::del($tagParams); + } + } + + // Delete entries from entity file table. if (!empty($cefIDs)) { $cefIDs = implode(',', $cefIDs); $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )"; @@ -273,23 +286,16 @@ class CRM_Core_BAO_File extends CRM_Core_DAO_File { } if (!empty($cfIDs)) { - // Delete file only if there no any entity using this file. $deleteFiles = []; foreach ($cfIDs as $fId => $fUri) { - //delete tags from entity tag table - $tagParams = [ - 'entity_table' => 'civicrm_file', - 'entity_id' => $fId, - ]; - - CRM_Core_BAO_EntityTag::del($tagParams); - + // Delete file only if there are no longer any entities using this file. if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) { unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri); $deleteFiles[$fId] = $fId; } } + // Delete entries from file table. if (!empty($deleteFiles)) { $deleteFiles = implode(',', $deleteFiles); $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )"; -- 2.25.1