About custom Doctrine types, see the official documentation. For default types see the official documentation as well.
- Installation
- Custom type registration
- Map property as an enum
- Create enum
- Register subtype enum
- NULL is NULL, not Enum
- Understand the basics
- Exceptions philosophy
composer.phar require doctrineum/scalar
or manually edit composer.json at your project and "require":
block (extend existing)
"require": {
"doctrineum/scalar": "dev-master"
}
By helper method
ScalarEnum::registerSelf(); // quick self-registration
Or manually using "magic" class::class constant
use Doctrineum\Scalar\ScalarEnumType;
// ...
Type::addType(ScalarEnumType::getTypeName(), ScalarEnumType::class);
Type::addType(BarScalarEnumType::getTypeName(), BarScalarEnumType::class);
Or manually by old fashion way
<?php
// in bootstrapping code
// ...
use Doctrine\DBAL\Types\Type;
use Doctrineum\Scalar\ScalarEnumType;
// ...
// Register type
Type::addType(ScalarEnumType::getTypeName(), '\Doctrineum\ScalarEnumType');
Type::addType(BarScalarEnumType::getTypeName(), '\Foo\BarScalarEnumType');
Or if your project uses Symfony2
# app/config/config.yml
doctrine:
dbal:
# ...
types:
scalar_enum: Doctrineum\Scalar\ScalarEnumType
bar: Foo\BarScalarEnumType
#...
<?php
class Foo
{
/** @Column(type="scalar_enum") */
protected $field;
}
<?php
use Doctrineum\Scalar\ScalarEnum;
$enum = ScalarEnum::getEnum('foo bar');
You can register infinite number of enums, which are built according to a regexp of your choice.
<?php
use Doctrineum\Scalar\ScalarEnumType;
ScalarEnumType::addSubTypeEnum('\Foo\Bar\YourSubTypeEnum', '~get me different enum for this value~');
// ...
$enum = $ScalarEnumType->convertToPHPValue('foo');
get_class($enum) === '\Doctrineum\Scalar\ScalarEnum'; // true
get_class($enum) === '\Foo\Bar\YourSubTypeEnum'; // false
$byRegexpDeterminedEnum = $ScalarEnumType->convertToPHPValue('And now get me different enum for this value.');
get_class($byRegexpDeterminedEnum) === '\Foo\Bar\YourSubTypeEnum'; // true
You can not create ScalarEnum with NULL value. Just use NULL directly for such column value.
Beware on using subtypes only when main enum is an abstract class. You have to resolve from-database-NULL->to-PHP-value conversion, or register subtype explicitly for NULL value (empty string respectively), otherwise fatal error by abstract class instance creation occurs.
There are two roles - the factory and the value.
- ScalarEnumType is the factory (as part of the Doctrine\DBAL\Types\Type family), building an ScalarEnum by following ScalarEnumType rules.
- ScalarEnum is the value holder, de facto singleton, represented by a class. And class, as you know, can do a lot of things, which makes enum more sexy then whole scalar value.
- Subtype is an ScalarEnumType, but ruled not just by type, but also by current value itself. One type can has any number of subtypes, in dependence on your imagination and used enum values.
Doctrineum adopts Granam exception hierarchy ideas. That means every exceptionable state is probably by a logic mistake, rather than a runtime situation.