Move Email, Address, etc. is_primary handling on delete to a hook
[civicrm-core.git] / CRM / Core / DAO.php
index 04c63d5a631c298d211be9efde0a8c44e3223e3e..774b3548b60e7997fe85c759ebf4a46b728c03bd 100644 (file)
@@ -962,9 +962,14 @@ class CRM_Core_DAO extends DB_DataObject {
     CRM_Utils_Hook::pre('delete', $entityName, $record['id'], $record);
     $instance = new $className();
     $instance->id = $record['id'];
-    if (!$instance->delete()) {
+    // Load complete object for the sake of hook_civicrm_post, below
+    $instance->find(TRUE);
+    if (!$instance || !$instance->delete()) {
       throw new CRM_Core_Exception("Could not delete {$entityName} id {$record['id']}");
     }
+    // For other operations this hook is passed an incomplete object and hook listeners can load if needed.
+    // But that's not possible with delete because it's gone from the database by the time this hook is called.
+    // So in this case the object has been pre-loaded so hook listeners have access to the complete record.
     CRM_Utils_Hook::post('delete', $entityName, $record['id'], $instance);
 
     return $instance;