From 25564af9b87e192fc61b5de1e3f2b42a1ed90910 Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 1 Aug 2023 19:00:16 -0400 Subject: [PATCH] DAO - Normalize null values in the writeRecord function to avoid subtle bugs Passing around the string 'null' was a terrible practice and can lead to bugs when you use an operator like `empty()` on the value. --- CRM/Core/DAO.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index f4ae7c3d8c..27cb3b417c 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -935,6 +935,13 @@ class CRM_Core_DAO extends DB_DataObject { } $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className); + // For legacy reasons, empty values would sometimes be passed around as the string 'null'. + // The DAO treats 'null' the same as '', and an empty string makes a lot more sense! + // For the sake of hooks, normalize these values. + $record = array_map(function ($value) { + return $value === 'null' ? '' : $value; + }, $record); + \CRM_Utils_Hook::pre($op, $entityName, $record[$idField] ?? NULL, $record); $fields = static::getSupportedFields(); $instance = new static(); -- 2.25.1