API exists() no longer true for nullifyIfEmpty if empty string

Optimise DBString::exists() to skip shortcodes
This commit is contained in:
Damian Mooyman 2017-04-03 16:29:48 +12:00
parent 2c5e482de0
commit 9be22701fd
4 changed files with 20 additions and 5 deletions

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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)

View File

@ -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());
}