Download the PHP package acelaya/doctrine-enum-type without Composer
On this page you can find all versions of the php package acelaya/doctrine-enum-type. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package doctrine-enum-type
Doctrine Enum Type
⚠️ This package is no longer relevant since PHP now has native enums and doctrine officially supports them
https://www.doctrine-project.org/2022/01/11/orm-2.11.html
This package provides a base implementation to define doctrine entity column types that are mapped to MyCLabs\Enum\Enum
objects. That class is defined in the fantastic myclabs/php-enum package.
Installation
The recommended installation method is by using composer
composer require acelaya/doctrine-enum-type
Usage
This package provides a Acelaya\Doctrine\Type\PhpEnumType
class that extends Doctrine\DBAL\Types\Type
. You can use it to easily map type names to concrete Enums.
The PhpEnumType
class will be used as the doctrine type for every property that is an enumeration.
Let's imagine we have this two enums.
And this entity, with a column of each entity type.
The column type of the action property is the FQCN of the Action
enum, and the gender column type is php_enum_gender. To get this working, you have to register the concrete column types, using the Acelaya\Doctrine\Type\PhpEnumType::registerEnumType
static method.
That will internally register a customized doctrine type. As you can see, it its possible to just pass the FQCN of the enum, making the type use it as the name, but you can also provide a different name.
Alternatively you can use the Acelaya\Doctrine\Type\PhpEnumType::registerEnumTypes
, which expects an array of enums to register.
With this method, elements with a string key will be registered with that name, and elements with integer key will use the value as the type name.
Do the same for each concrete enum you want to register.
If you need more information on custom doctrine column types, read this http://doctrine-orm.readthedocs.io/en/latest/cookbook/custom-mapping-types.html
Customize SQL declaration
By default, the Acelaya\Doctrine\Type\PhpEnumType
class defines all enums as a VARCHAR like this:
This means that you can customize the length
(which defaults to 255), but not the column type.
If you want something more specific, like a MySQL enum, just extend PhpEnumType
and overwrite the getSQLDeclaration()
method with something like this.
Then remember to register the enums with your own class.
Customize value casting
The library assumes all the values of the enumeration are strings, but that might not always be the case.
Since v2.2.0, the library provides a way to define hooks which customize how values are cast from the database and to the database, by using the castValueIn
and castValueOut
static methods in the enumeration.
For example, let's imagine we have this enum:
Using the generic PhpEnumType
would cause an error when casting the value from the database, since it will be deserialized as a string.
In order to get it working, you have to add the castValueIn
static method in the enum:
This method is automatically invoked before checking if the value is valid, and creating the enum instance.
The same way, a castValueOut
method can be defined in order to modify the value before getting it persisted into the database.
These methods can also be used to customize the values. For example, if you used to have values with underscores and want to replace them by dashes, you could define these methods in your enum, in order to keep backward compatibility.