Details
-
Type:
Improvement
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.2.1
-
Fix Version/s: 1.2.3
-
Component/s: None
-
Labels:None
-
Environment:Any
Description
At the moment:
- Doctrine_Table::enumIndex($fieldName, $value) returns $value specified as a parameter if no index for it is found
- Doctrine_Table::enumValue($fieldName, $index) returns $index specified as a parameter if no value for it is found
This actually makes writing code which deals with enums a bit difficult as one would like to detect incorrect parameter as soon as possible (e.g. not when Doctrine validation kicks in and checks that enum value is incorrect).
Current algorithm that returns index or value is not sufficient to determine error in situation when index=value, e.g. enum values are [0, 1, 2, 3]. Calling e.g. enumValue('sample', 4) would yield 4 - so it makes difficult to determine whether it's correct value.
I hope this makes sense, let me know if anything is unclear.
The workaround for this is to check whether mapped index/value exists in array returned by Doctrine_Table::getEnumValues(), but this is an extra unnecessary check.
So instead of writing:
$index = 5;
$someTable = Doctrine_Core::getTable('Sample');
$value = $someTable->enumValue('sample', $index);
$values = $someTable->getEnumValues('sample');
if (in_array($value, $values) == false)
{
throw new UnexpectedValueException('illegal index specified!');
}
I'd like to write:
$index = 5;
$value = Doctrine_Core::getTable('Sample')->enumValue('sample', $index);
if ($value === false)
{
throw new UnexpectedValueException('illegal index specified!');
}
Simpler, eh?
Please find patches for Doctrine/Table.php and Doctrine/Column.php against their versions from rev. 7298
Patches includes proposed change (returning false instead of original parameter).
Interestingly, Doctrine_Column::enumIndex() returns FALSE when index is not found for specified value as it uses array_search().
So - another reason why this processing should be unified