diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 0d0910aeb05..9886c10dd6e 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -3150,7 +3150,10 @@ public function dbObject($fieldName) 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 92c7b142f63..8ad2cc166e5 100644 --- a/tests/php/ORM/DataObjectTest.php +++ b/tests/php/ORM/DataObjectTest.php @@ -2622,4 +2622,15 @@ public function testDataObjectCreationHydrateWithoutID() '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 96fc7e6649f..0ad04794b51 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 = [