From faf37f984408b4645ebf1b1b87b608aa009aa8fa Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 16 Aug 2022 22:44:00 -0400 Subject: [PATCH] APIv4 - Fix some hard-coded references to 'id' in DAO action classes --- Civi/Api4/Generic/DAOCreateAction.php | 7 +++++-- Civi/Api4/Generic/DAODeleteAction.php | 13 +++++++------ Civi/Api4/Generic/DAOSaveAction.php | 5 ++++- Civi/Api4/Generic/Traits/DAOActionTrait.php | 3 ++- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Civi/Api4/Generic/DAOCreateAction.php b/Civi/Api4/Generic/DAOCreateAction.php index 14914175bc..b40a67d448 100644 --- a/Civi/Api4/Generic/DAOCreateAction.php +++ b/Civi/Api4/Generic/DAOCreateAction.php @@ -12,6 +12,8 @@ namespace Civi\Api4\Generic; +use Civi\Api4\Utils\CoreUtil; + /** * Create a new $ENTITY from supplied values. * @@ -37,8 +39,9 @@ class DAOCreateAction extends AbstractCreateAction { * @throws \API_Exception */ protected function validateValues() { - if (!empty($this->values['id'])) { - throw new \API_Exception('Cannot pass id to Create action. Use Update action instead.'); + $idField = CoreUtil::getIdFieldName($this->getEntityName()); + if (!empty($this->values[$idField])) { + throw new \API_Exception("Cannot pass $idField to Create action. Use Update action instead."); } parent::validateValues(); } diff --git a/Civi/Api4/Generic/DAODeleteAction.php b/Civi/Api4/Generic/DAODeleteAction.php index 84d818de9b..3dbab24757 100644 --- a/Civi/Api4/Generic/DAODeleteAction.php +++ b/Civi/Api4/Generic/DAODeleteAction.php @@ -54,28 +54,29 @@ class DAODeleteAction extends AbstractBatchAction { * @throws \API_Exception */ protected function deleteObjects($items) { - $ids = []; + $idField = CoreUtil::getIdFieldName($this->getEntityName()); + $result = []; $baoName = $this->getBaoName(); // Use BAO::del() method if it is not deprecated if (method_exists($baoName, 'del') && !ReflectionUtils::isMethodDeprecated($baoName, 'del')) { foreach ($items as $item) { - $args = [$item['id']]; + $args = [$item[$idField]]; $bao = call_user_func_array([$baoName, 'del'], $args); if ($bao !== FALSE) { - $ids[] = ['id' => $item['id']]; + $result[] = [$idField => $item[$idField]]; } else { - throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); + throw new \API_Exception("Could not delete {$this->getEntityName()} $idField {$item[$idField]}"); } } } else { foreach ($baoName::deleteRecords($items) as $instance) { - $ids[] = ['id' => $instance->id]; + $result[] = [$idField => $instance->$idField]; } } - return $ids; + return $result; } } diff --git a/Civi/Api4/Generic/DAOSaveAction.php b/Civi/Api4/Generic/DAOSaveAction.php index 7d6d6b709f..f79bef33eb 100644 --- a/Civi/Api4/Generic/DAOSaveAction.php +++ b/Civi/Api4/Generic/DAOSaveAction.php @@ -12,6 +12,8 @@ namespace Civi\Api4\Generic; +use Civi\Api4\Utils\CoreUtil; + /** * @inheritDoc */ @@ -22,11 +24,12 @@ class DAOSaveAction extends AbstractSaveAction { * @inheritDoc */ public function _run(Result $result) { + $idField = CoreUtil::getIdFieldName($this->getEntityName()); foreach ($this->records as &$record) { $record += $this->defaults; $this->formatWriteValues($record); $this->matchExisting($record); - if (empty($record['id'])) { + if (empty($record[$idField])) { $this->fillDefaults($record); } } diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index 94e0d7af21..491d33036d 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -121,9 +121,10 @@ trait DAOActionTrait { } $result = []; + $idField = CoreUtil::getIdFieldName($this->getEntityName()); foreach ($items as &$item) { - $entityId = $item['id'] ?? NULL; + $entityId = $item[$idField] ?? NULL; FormattingUtil::formatWriteParams($item, $this->entityFields()); $this->formatCustomParams($item, $entityId); -- 2.25.1