From 9be22701fd31123e4cb63102c04c6eb4b82b8792 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 3 Apr 2017 16:29:48 +1200 Subject: [PATCH] API exists() no longer true for nullifyIfEmpty if empty string Optimise DBString::exists() to skip shortcodes --- src/ORM/FieldType/DBHTMLText.php | 8 ++++++++ src/ORM/FieldType/DBHTMLVarchar.php | 8 ++++++++ src/ORM/FieldType/DBString.php | 5 ++--- tests/php/ORM/DBFieldTest.php | 4 ++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ORM/FieldType/DBHTMLText.php b/src/ORM/FieldType/DBHTMLText.php index bc7c83fb4..c3624a701 100644 --- a/src/ORM/FieldType/DBHTMLText.php +++ b/src/ORM/FieldType/DBHTMLText.php @@ -242,4 +242,12 @@ class DBHTMLText extends DBText } return null; } + + public function exists() + { + // Optimisation: don't process shortcode just for ->exists() + $value = $this->getValue(); + // All truthy values and non-empty strings exist ('0' but not (int)0) + return $value || (is_string($value) && strlen($value)); + } } diff --git a/src/ORM/FieldType/DBHTMLVarchar.php b/src/ORM/FieldType/DBHTMLVarchar.php index 4ce7ebc4a..561ad4c4c 100644 --- a/src/ORM/FieldType/DBHTMLVarchar.php +++ b/src/ORM/FieldType/DBHTMLVarchar.php @@ -141,4 +141,12 @@ class DBHTMLVarchar extends DBVarchar } return null; } + + public function exists() + { + // Optimisation: don't process shortcode just for ->exists() + $value = $this->getValue(); + // All truthy values and non-empty strings exist ('0' but not (int)0) + return $value || (is_string($value) && strlen($value)); + } } diff --git a/src/ORM/FieldType/DBString.php b/src/ORM/FieldType/DBString.php index cdfeb9b8f..de5e4730b 100644 --- a/src/ORM/FieldType/DBString.php +++ b/src/ORM/FieldType/DBString.php @@ -96,9 +96,8 @@ abstract class DBString extends DBField public function exists() { $value = $this->RAW(); - return $value // All truthy values exist - || (is_string($value) && strlen($value)) // non-empty strings exist ('0' but not (int)0) - || (!$this->getNullifyEmpty() && $value === ''); // Remove this stupid exemption in 4.0 + // All truthy values and non-empty strings exist ('0' but not (int)0) + return $value || (is_string($value) && strlen($value)); } public function prepValueForDB($value) diff --git a/tests/php/ORM/DBFieldTest.php b/tests/php/ORM/DBFieldTest.php index 2d9c88f28..8946d72c9 100644 --- a/tests/php/ORM/DBFieldTest.php +++ b/tests/php/ORM/DBFieldTest.php @@ -205,7 +205,7 @@ class DBFieldTest extends SapphireTest $varcharField->setValue('abc'); $this->assertTrue($varcharField->exists()); $varcharField->setValue(''); - $this->assertTrue($varcharField->exists()); + $this->assertFalse($varcharField->exists()); $varcharField->setValue(null); $this->assertFalse($varcharField->exists()); @@ -223,7 +223,7 @@ class DBFieldTest extends SapphireTest $textField->setValue('abc'); $this->assertTrue($textField->exists()); $textField->setValue(''); - $this->assertTrue($textField->exists()); + $this->assertFalse($textField->exists()); $textField->setValue(null); $this->assertFalse($textField->exists()); }