Skip to content

Commit

Permalink
Merge pull request #10005 from creative-commoners/pulls/4.8/10k
Browse files Browse the repository at this point in the history
FIX Parse Enums with dots in their values
  • Loading branch information
chillu authored Jul 1, 2021
2 parents abf72c3 + 8e803bb commit e8c14a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/php/ORM/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
4 changes: 4 additions & 0 deletions tests/php/ORM/DataObjectTest/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit e8c14a9

Please sign in to comment.