From 32b7fa58bd4eb4e18b427e9ba2bbbd9ac0064ab7 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 26 Sep 2022 11:51:50 -0400 Subject: [PATCH] use real null for primary keys --- CRM/Core/DAO.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index f58eaf2e4e..a64287c81c 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -778,6 +778,7 @@ class CRM_Core_DAO extends DB_DataObject { */ public function copyValues($params) { $allNull = TRUE; + $primaryKey = $this->getFirstPrimaryKey(); foreach ($this->fields() as $uniqueName => $field) { $dbName = $field['name']; if (array_key_exists($dbName, $params)) { @@ -795,7 +796,19 @@ class CRM_Core_DAO extends DB_DataObject { // if there is no value then make the variable NULL if ($exists) { if ($value === '') { - $this->$dbName = 'null'; + if ($dbName === $primaryKey && $field['type'] === CRM_Utils_Type::T_INT) { + // See also \Civi\Api4\Utils\FormattingUtil::formatWriteParams(). + // The string 'null' is used in pear::db to "unset" values, whereas + // it skips over fields where the param is real null. However + // "unsetting" a primary key doesn't make sense - you can't convert + // an existing record to a "new" one. And then having string 'null' + // in the dao object can confuse later code, in particular save() + // which then calls the update hook instead of the create hook. + $this->$dbName = NULL; + } + else { + $this->$dbName = 'null'; + } } elseif (is_array($value) && !empty($field['serialize'])) { $this->$dbName = CRM_Core_DAO::serializeField($value, $field['serialize']); -- 2.25.1