From eda5da742e9f2d374a7c55b4578a1f0a1fd8c4c1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 1 Jul 2021 22:34:01 -0400 Subject: [PATCH] Add primaryKey info to daos --- CRM/Core/DAO.php | 7 +++++++ Civi/Api4/Generic/AbstractEntity.php | 1 + xml/templates/dao.tpl | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 253c58d580d5..5bd931c91c2f 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -32,6 +32,13 @@ */ class CRM_Core_DAO extends DB_DataObject { + /** + * Primary key field(s). + * + * @var string[] + */ + public static $_primaryKey = ['id']; + /** * How many times has this instance been cloned. * diff --git a/Civi/Api4/Generic/AbstractEntity.php b/Civi/Api4/Generic/AbstractEntity.php index b218cec75e4a..266c6307a491 100644 --- a/Civi/Api4/Generic/AbstractEntity.php +++ b/Civi/Api4/Generic/AbstractEntity.php @@ -152,6 +152,7 @@ public static function getInfo() { $dao = \CRM_Core_DAO_AllCoreTables::getFullName($info['name']); if ($dao) { $info['paths'] = $dao::getEntityPaths(); + $info['primary_key'] = $dao::$_primaryKey; $info['icon'] = $dao::$_icon; $info['label_field'] = $dao::$_labelField; $info['dao'] = $dao; diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index 7aacd67d3aa8..f80ab793ac14 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -24,6 +24,16 @@ class {$table.className} extends CRM_Core_DAO {ldelim} */ public static $_tableName = '{$table.name}'; + {* Only print this variable if it's different than the default in CRM_Core_DAO *} + {if count($table.primaryKey.field) !== 1 || $table.primaryKey.field.0 !== 'id'} + /** + * Primary key field(s). + * + * @var string[] + */ + public static $_primaryKey = [{if $table.primaryKey.field}'{"', '"|implode:$table.primaryKey.field}'{/if}]; + {/if} + {if $table.icon} /** * Icon associated with this entity.