From 12e4505a2193162253eb5a1132af87b0c65bf2b9 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 15 Aug 2021 11:29:43 -0400 Subject: [PATCH] APIv4 - Add util function to get name of id field (primary key) --- Civi/Api4/Generic/AbstractSaveAction.php | 2 +- Civi/Api4/Generic/BasicSaveAction.php | 4 +++- Civi/Api4/Query/Api4SelectQuery.php | 4 ++-- Civi/Api4/Utils/CoreUtil.php | 9 +++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Civi/Api4/Generic/AbstractSaveAction.php b/Civi/Api4/Generic/AbstractSaveAction.php index c815645abb..3892e4f1b2 100644 --- a/Civi/Api4/Generic/AbstractSaveAction.php +++ b/Civi/Api4/Generic/AbstractSaveAction.php @@ -74,7 +74,7 @@ abstract class AbstractSaveAction extends AbstractAction { * @throws \Civi\API\Exception\UnauthorizedException */ protected function validateValues() { - $idField = $this->getIdField(); + $idField = CoreUtil::getIdFieldName($this->getEntityName()); // FIXME: There should be a protocol to report a full list of errors... Perhaps a subclass of API_Exception? $unmatched = []; foreach ($this->records as $record) { diff --git a/Civi/Api4/Generic/BasicSaveAction.php b/Civi/Api4/Generic/BasicSaveAction.php index f0e175e5fa..b886b50e3e 100644 --- a/Civi/Api4/Generic/BasicSaveAction.php +++ b/Civi/Api4/Generic/BasicSaveAction.php @@ -13,6 +13,7 @@ namespace Civi\Api4\Generic; use Civi\API\Exception\NotImplementedException; +use Civi\Api4\Utils\CoreUtil; /** * @inheritDoc @@ -51,6 +52,7 @@ class BasicSaveAction extends AbstractSaveAction { * @param \Civi\Api4\Generic\Result $result */ public function _run(Result $result) { + $idField = CoreUtil::getIdFieldName($this->getEntityName()); foreach ($this->records as &$record) { $record += $this->defaults; $this->formatWriteValues($record); @@ -64,7 +66,7 @@ class BasicSaveAction extends AbstractSaveAction { $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]); $get ->setCheckPermissions($this->getCheckPermissions()) - ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField())); + ->addWhere($idField, 'IN', (array) $result->column($idField)); $result->exchangeArray((array) $get->execute()); } } diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 686253a331..119abe49a9 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -245,7 +245,7 @@ class Api4SelectQuery { // If the joined_entity.id isn't in the fieldspec already, autoJoinFK will attempt to add the entity. $fkField = substr($wildField, 0, strrpos($wildField, '.')); $fkEntity = $this->getField($fkField)['fk_entity'] ?? NULL; - $id = $fkEntity ? CoreUtil::getInfoItem($fkEntity, 'primary_key')[0] : 'id'; + $id = $fkEntity ? CoreUtil::getIdFieldName($fkEntity) : 'id'; $this->autoJoinFK($fkField . ".$id"); $matches = $this->selectMatchingFields($wildField); array_splice($select, $pos, 1, $matches); @@ -1101,7 +1101,7 @@ class Api4SelectQuery { */ public static function renderSerializedJoin(array $field): string { $sep = \CRM_Core_DAO::VALUE_SEPARATOR; - $id = CoreUtil::getInfoItem($field['entity'], 'primary_key')[0]; + $id = CoreUtil::getIdFieldName($field['entity']); $searchFn = "FIND_IN_SET(`{$field['table_name']}`.`$id`, REPLACE({$field['sql_name']}, '$sep', ','))"; return "( SELECT GROUP_CONCAT( diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index 0c9f9612a6..efb11f48e3 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -64,6 +64,15 @@ class CoreUtil { return $className ? $className::getInfo()[$keyToReturn] ?? NULL : NULL; } + /** + * Get name of unique identifier, typically "id" + * @param string $entityName + * @return string + */ + public static function getIdFieldName(string $entityName): string { + return self::getInfoItem($entityName, 'primary_key')[0] ?? 'id'; + } + /** * Get table name of given entity * -- 2.25.1