diff --git a/CRM/Core/CodeGen/Specification.php b/CRM/Core/CodeGen/Specification.php index f2300887972b..dfc382694346 100644 --- a/CRM/Core/CodeGen/Specification.php +++ b/CRM/Core/CodeGen/Specification.php @@ -228,6 +228,7 @@ public function getTable($tableXML, &$database, &$tables) { 'comment' => $this->value('comment', $tableXML), 'description' => $this->value('description', $tableXML), 'localizable' => $localizable, + 'primary_key' => (array) $tableXML->primaryKey->name, 'log' => $this->value('log', $tableXML, 'false'), 'archive' => $this->value('archive', $tableXML, 'false'), ]; 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..b56b347f011b 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -18,12 +18,22 @@ class {$table.className} extends CRM_Core_DAO {ldelim} {if !empty($table.component)}const COMPONENT = '{$table.component}';{/if} /** - * Static instance to hold the table name. + * Name of SQL table for the {$table.entity} entity. * * @var string */ public static $_tableName = '{$table.name}'; + {* Only print this variable if it's different than the default in CRM_Core_DAO *} + {if count($table.primary_key) > 1 || $table.primary_key.0 !== 'id'} + /** + * Primary key field(s). + * + * @var string[] + */ + public static $_primaryKey = ['{"', '"|implode:$table.primary_key}']; + {/if} + {if $table.icon} /** * Icon associated with this entity.