From 8e803bbcfcd542e58cab667d0789873270c8547a Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 1 Jul 2021 16:00:08 +1200 Subject: [PATCH] FIX Parse Enums with dots in their values --- src/ORM/DataObject.php | 5 ++++- tests/php/ORM/DataObjectTest.php | 11 +++++++++++ tests/php/ORM/DataObjectTest/Fixture.php | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 0d0910aeb..9886c10dd 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -3150,7 +3150,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity return $value; } - list($class, $spec) = explode('.', $helper); + $pos = strpos($helper, '.'); + $class = substr($helper, 0, $pos); + $spec = substr($helper, $pos + 1); + /** @var DBField $obj */ $table = $schema->tableName($class); $obj = Injector::inst()->create($spec, $fieldName); diff --git a/tests/php/ORM/DataObjectTest.php b/tests/php/ORM/DataObjectTest.php index 92c7b142f..8ad2cc166 100644 --- a/tests/php/ORM/DataObjectTest.php +++ b/tests/php/ORM/DataObjectTest.php @@ -2622,4 +2622,15 @@ class DataObjectTest extends SapphireTest 'Salary' => 50, ], DataObject::CREATE_HYDRATED); } + + public function testDBObjectEnum() + { + $obj = new DataObjectTest\Fixture(); + // enums are parsed correctly + $vals = ['25', '50', '75', '100']; + $this->assertSame(array_combine($vals, $vals), $obj->dbObject('MyEnum')->enumValues()); + // enum with dots in their values are also parsed correctly + $vals = ['25.25', '50.00', '75.00', '100.50']; + $this->assertSame(array_combine($vals, $vals), $obj->dbObject('MyEnumWithDots')->enumValues()); + } } diff --git a/tests/php/ORM/DataObjectTest/Fixture.php b/tests/php/ORM/DataObjectTest/Fixture.php index 96fc7e664..0ad04794b 100644 --- a/tests/php/ORM/DataObjectTest/Fixture.php +++ b/tests/php/ORM/DataObjectTest/Fixture.php @@ -25,6 +25,10 @@ class Fixture extends DataObject implements TestOnly 'MyInt' => 'Int', 'MyCurrency' => 'Currency', 'MyDecimal'=> 'Decimal', + + // Enums + 'MyEnum' => 'Enum("25,50,75,100", "50")', + 'MyEnumWithDots' => 'Enum("25.25,50.00,75.00,100.50", "50.00")', ]; private static $defaults = [