FIX Parse Enums with dots in their values

This commit is contained in:
Steve Boyd 2021-07-01 16:00:08 +12:00
parent e812999632
commit 8e803bbcfc
3 changed files with 19 additions and 1 deletions

View File

@ -3150,7 +3150,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $value; return $value;
} }
list($class, $spec) = explode('.', $helper); $pos = strpos($helper, '.');
$class = substr($helper, 0, $pos);
$spec = substr($helper, $pos + 1);
/** @var DBField $obj */ /** @var DBField $obj */
$table = $schema->tableName($class); $table = $schema->tableName($class);
$obj = Injector::inst()->create($spec, $fieldName); $obj = Injector::inst()->create($spec, $fieldName);

View File

@ -2622,4 +2622,15 @@ class DataObjectTest extends SapphireTest
'Salary' => 50, 'Salary' => 50,
], DataObject::CREATE_HYDRATED); ], 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());
}
} }

View File

@ -25,6 +25,10 @@ class Fixture extends DataObject implements TestOnly
'MyInt' => 'Int', 'MyInt' => 'Int',
'MyCurrency' => 'Currency', 'MyCurrency' => 'Currency',
'MyDecimal'=> 'Decimal', '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 = [ private static $defaults = [