https://github.com/zlikavac32/php-enum-doctrine
Doctrine support for zlikavac32/php-enum
https://github.com/zlikavac32/php-enum-doctrine
doctrine doctrine-enum enum enumeration php php-enum
Last synced: 6 months ago
JSON representation
Doctrine support for zlikavac32/php-enum
- Host: GitHub
- URL: https://github.com/zlikavac32/php-enum-doctrine
- Owner: zlikavac32
- License: mit
- Created: 2017-11-16T20:38:18.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-13T02:10:34.000Z (almost 6 years ago)
- Last Synced: 2025-08-16T14:50:38.738Z (7 months ago)
- Topics: doctrine, doctrine-enum, enum, enumeration, php, php-enum
- Language: PHP
- Size: 19.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PHP Enum Doctrine
[](https://travis-ci.org/zlikavac32/php-enum-doctrine) [](https://packagist.org/packages/zlikavac32/php-enum-doctrine) [](https://packagist.org/packages/zlikavac32/php-enum-doctrine) [](https://scrutinizer-ci.com/g/zlikavac32/php-enum-doctrine/?branch=master) [](https://www.codacy.com/app/zlikavac32/php-enum-doctrine?utm_source=github.com&utm_medium=referral&utm_content=zlikavac32/php-enum-doctrine&utm_campaign=Badge_Grade)
Doctrine support for [zlikavac32/php-enum](https://github.com/zlikavac32/php-enum).
## Table of contents
1. [Installation](#installation)
1. [Usage](#usage)
1. [Custom column length](#custom-column-length)
1. [Custom representation](#custom-representation)
1. [Limitations](#limitations)
1. [Further work](#further-work)
## Installation
Recommended installation is through Composer.
```
composer require zlikavac32/php-enum-doctrine
```
## Usage
Assumption is that there exists a valid enum `\YesNoEnum`.
Create a new type that extends `\Zlikavac32\DoctrineEnum\DBAL\Types\EnumType`.
```php
use Zlikavac32\DoctrineEnum\DBAL\Types\EnumType;
class YesNoEnumType extends EnumType
{
// ...
}
```
Next, define `protected function enumClass(): string`. This method should return FQN of the enum class that this type exposes to the Doctrine.
```php
protected function enumClass(): string
{
return \YesNoEnum::class;
}
```
Define Doctrine method `public function getName(): string` that defines type's name.
```php
public function getName(): string
{
return 'enum_yes_no';
}
```
And that's it. Only thing left to do is to register the type using
```php
\Doctrine\DBAL\Types\Type::addType('enum_yes_no', \YesNoEnumType::class);
```
You can now use `enum_yes_no` type.
```php
/**
* @Column(type="enum_yes_no", nullable=true)
* @var \YesNoEnum|null
*/
private $yesNo;
```
For more info on the custom Doctrine mapping types, check [official documentation](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types).
### Custom column length
Internally this library uses `varchar` type with the maximum length of `32`. If you want to fit the length to your own needs, just override method `protected function columnLength(): int`.
```php
protected function columnLength(): int
{
return 16;
}
```
Note that on types first usage, all enum elements names are checked against specified column length. If a name longer than maximum length is detected, a `\LogicException` is thrown.
### Custom representation
By default, name of the enum element is used for it's representation in the database. To change that behaviour, override methods `enumToDatabaseValue()` and `databaseValueToEnum()`.
## Limitations
This library does not use platform dependent types like `enum` in `MySQL` or custom types in `PostgresSQL`. Instead, `varchar` is used.
Reasons for this are:
- `Doctrine` can not diff enum contents because that's types intrinsic property
- for `PostgresSQL` we can't diff column because type is not in Doctrine control
- column constraints can not be used because they break `ALTER` syntax
If you know how to avoid any of this, please let me know.
## Further work
Figure out how to overcome issues in [Limitations](#limitations).