From 615f874077783f799c64d091c0e8882d8e615579 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 16 Aug 2022 19:34:26 +1200 Subject: [PATCH] Update DAO to use declared primary field, do not assume... --- CRM/Core/DAO.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index be52f2b24a..55b353c617 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -39,6 +39,13 @@ class CRM_Core_DAO extends DB_DataObject { */ public static $_primaryKey = ['id']; + /** + * @return string[] + */ + protected function getPrimaryKey(): array { + return static::$_primaryKey; + } + /** * How many times has this instance been cloned. * @@ -536,7 +543,7 @@ class CRM_Core_DAO extends DB_DataObject { public function sequenceKey() { static $sequenceKeys; if (!isset($sequenceKeys)) { - $sequenceKeys = ['id', TRUE]; + $sequenceKeys = [$this->getPrimaryKey()[0], TRUE]; } return $sequenceKeys; } @@ -634,11 +641,15 @@ class CRM_Core_DAO extends DB_DataObject { */ public function save($hook = TRUE) { $eventID = uniqid(); - if (!empty($this->id)) { + // In practice the 'Import' entities are probably the only ones with a single + // primary key that is not import. Should we check all if more than one? + // Can do that when it comes up... + $primaryField = $this->getPrimaryKey()[0]; + if (!empty($this->$primaryField)) { if ($hook) { $preEvent = new \Civi\Core\DAO\Event\PreUpdate($this); $preEvent->eventID = $eventID; - \Civi::dispatcher()->dispatch("civi.dao.preUpdate", $preEvent); + \Civi::dispatcher()->dispatch('civi.dao.preUpdate', $preEvent); } $result = $this->update(); @@ -646,7 +657,7 @@ class CRM_Core_DAO extends DB_DataObject { if ($hook) { $event = new \Civi\Core\DAO\Event\PostUpdate($this, $result); $event->eventID = $eventID; - \Civi::dispatcher()->dispatch("civi.dao.postUpdate", $event); + \Civi::dispatcher()->dispatch('civi.dao.postUpdate', $event); } $this->clearDbColumnValueCache(); } -- 2.25.1