Find DAO by ID instead of just calling the constructor and setting the ID
[civicrm-core.git] / Civi / Api4 / Generic / ExportAction.php
index aed9e322a42bb27b9dbb1278bb35078612c82fbc..b7e73717960ef36592aed1f02ee26d5f1b4ab2c5 100644 (file)
@@ -81,21 +81,19 @@ class ExportAction extends AbstractAction {
     foreach ($allFields as $field) {
       // Use implicit join syntax but only if the fk entity has a `name` field
       if (!empty($field['fk_entity']) && array_key_exists('name', $this->getFieldsForExport($field['fk_entity']))) {
+        $select[] = $field['name'];
         $select[] = $field['name'] . '.name';
         $pseudofields[$field['name'] . '.name'] = $field['name'];
       }
       // Use pseudoconstant syntax if appropriate
       elseif ($this->shouldUsePseudoconstant($entityType, $field)) {
+        $select[] = $field['name'];
         $select[] = $field['name'] . ':name';
         $pseudofields[$field['name'] . ':name'] = $field['name'];
       }
       elseif (empty($field['fk_entity'])) {
         $select[] = $field['name'];
       }
-      // Needed for exporting the option group for a custom field
-      if ($entityType === 'CustomField' && ($field['fk_entity'] ?? NULL) === 'OptionGroup') {
-        $select[] = $field['name'];
-      }
     }
     $record = civicrm_api4($entityType, 'get', [
       'checkPermissions' => $this->checkPermissions,
@@ -107,13 +105,6 @@ class ExportAction extends AbstractAction {
     }
     // The get api always returns ID, but it should not be included in an export
     unset($record['id']);
-    // Null fields should not use joins/pseudoconstants
-    foreach ($pseudofields as $alias => $fieldName) {
-      if (is_null($record[$alias])) {
-        unset($record[$alias]);
-        $record[$fieldName] = NULL;
-      }
-    }
     // Should references be limited to the current domain?
     $limitRefsByDomain = $entityType === 'OptionGroup' && \CRM_Core_OptionGroup::isDomainOptionGroup($record['name']) ? \CRM_Core_BAO_Domain::getDomain()->id : FALSE;
     foreach ($allFields as $fieldName => $field) {
@@ -144,7 +135,15 @@ class ExportAction extends AbstractAction {
       ) {
         $this->exportRecord('OptionGroup', $record['option_group_id'], $result);
       }
-      unset($record['option_group_id']);
+    }
+    // Don't use joins/pseudoconstants if null or if it has the same value as the original
+    foreach ($pseudofields as $alias => $fieldName) {
+      if (!isset($record[$alias]) || $record[$alias] == ($record[$fieldName] ?? NULL)) {
+        unset($record[$alias]);
+      }
+      else {
+        unset($record[$fieldName]);
+      }
     }
     $result[] = [
       'name' => $name,
@@ -160,8 +159,7 @@ class ExportAction extends AbstractAction {
     $daoName = CoreUtil::getInfoItem($entityType, 'dao');
     if ($daoName) {
       /** @var \CRM_Core_DAO $dao */
-      $dao = new $daoName();
-      $dao->id = $entityId;
+      $dao = $daoName::findById($entityId);
       // Collect references into arrays keyed by entity type
       $references = [];
       foreach ($dao->findReferences() as $reference) {
@@ -221,6 +219,10 @@ class ExportAction extends AbstractAction {
       return FALSE;
     }
     $daoName = CoreUtil::getInfoItem($entityType, 'dao');
+    // Exception for Profile.field_name
+    if ($entityType === 'UFField' && $field['name'] === 'field_name') {
+      return TRUE;
+    }
     // Options generated by a callback function tend to be stable,
     // and the :name property may not be reliable. Use plain value.
     if ($daoName && !empty($daoName::getSupportedFields()[$field['name']]['pseudoconstant']['callback'])) {