From 4b4fbabed5d70bf577e4b0d6fdbc9dab9da80451 Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Thu, 8 Nov 2018 14:36:16 +1300 Subject: [PATCH] FIX TreeMultiselectField passes value 'unchanged' as null to ORM for 'ID' column key --- src/Forms/TreeMultiselectField.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Forms/TreeMultiselectField.php b/src/Forms/TreeMultiselectField.php index 72de4dc11..1c0166f64 100644 --- a/src/Forms/TreeMultiselectField.php +++ b/src/Forms/TreeMultiselectField.php @@ -261,4 +261,30 @@ class TreeMultiselectField extends TreeDropdownField $copy->setTitleField($this->getTitleField()); return $copy; } + + /** + * {@inheritdoc} + * + * @deprecated 4.0..5.0 + */ + protected function objectForKey($key) + { + /** + * Fixes https://github.com/silverstripe/silverstripe-framework/issues/8332 + * + * Due to historic reasons, the default (empty) value for this field is 'unchanged', even though + * the field is usually integer on the database side. + * MySQL handles that gracefully and returns an empty result in that case, + * whereas some other databases (e.g. PostgreSQL) do not support comparison + * of numeric types with string values, issuing a database error. + * + * This fix is not ideal, but supposed to keep backward compatibility for SS4. + * Since SS5 this method should be removed and NULL should be used instead of 'unchanged'. + */ + if ($this->getKeyField() ==='ID' && $key === 'unchanged') { + $key = null; + } + + return parent::objectForKey($key); + } }