`CRM_Core_DAO::copyValues()` already converts '' to 'null', and passing the string 'null'
into `BAO::writeRecords` or `BAO::create` functions prematurely can introduce subtle bugs
because the string 'null' is `!empty()` whereas '' is `empty()`.
else {
$inputFields = array_keys($input);
// Convert 'null' input to true null
- foreach ($input as $key => $val) {
- if ($val === 'null') {
+ foreach ($inputFields as $key) {
+ if (($bao->$key ?? NULL) === 'null') {
$bao->$key = NULL;
}
}
/*
* Because of the wacky way that database values are saved we need to format
* some of the values here. In this strange world the string 'null' is used to
- * unset values. Hence if we encounter true null we change it to string 'null'.
+ * unset values. If we encounter true null at this layer we change it to an empty string
+ * and it will be converted to 'null' by CRM_Core_DAO::copyValues.
*
* If we encounter the string 'null' then we assume the user actually wants to
* set the value to string null. However since the string null is reserved for
* 'Null'.
*/
elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
- $params[$name] = 'null';
+ $params[$name] = '';
}
}