Cannot set custom field values to string 'NULL'
Let's have a non-null conversation about saving string 'NULL' values.
While upgrading a site from 4.6 to 5.6 we noticed that one of the custom fields (select widget) which has an option value of the string 'NULL' was not being updated on save.
Traced it down to: https://github.com/civicrm/civicrm-core/blob/master/CRM/Core/BAO/CustomValueTable.php#L222
This behavior came into the system back in 4.7-alpha1 https://github.com/civicrm/civicrm-core/commit/fd630ef95ece7c8f8005ddaf0e0e8cc391c1be43
Turns out any alphanumeric field type widget won't allow the string value of 'NULL' to be saved..it is converted to the really non-string NULL value.
Turns out it's an easy fix for us to just exclude this behavior by:
if (strtolower($value) === "null" && $value != "NULL") {
// when unsetting a value to null, we don't need to validate the type
// https://projectllr.atlassian.net/browse/VGQBMP-20
$set[$field['column_name']] = $value;
}
But I wonder, why is it that strings are converted to real NULLs?
Is this a bug or a feature?
What are the potential consequences of this patch?
Are there good reasons to convert strings to NULL ?
Would this patch break other expected behavior in Civi?
What about strings like 'Null' or 'nulL' etc...