zlikavac32/php-enum-doctrine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zlikavac32/php-enum-doctrine

Composer 安装命令:

composer require zlikavac32/php-enum-doctrine

包简介

Doctrine PHP enum support

README 文档

README

Build Status Latest Stable Version License Scrutinizer Code Quality Codacy Badge

Doctrine support for zlikavac32/php-enum.

Table of contents

  1. Installation
  2. Usage
    1. Custom column length
    2. Custom representation
  3. Limitations
  4. 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.

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.

protected function enumClass(): string
{
    return \YesNoEnum::class;
}

Define Doctrine method public function getName(): string that defines type's name.

public function getName(): string
{
    return 'enum_yes_no';
}

And that's it. Only thing left to do is to register the type using

\Doctrine\DBAL\Types\Type::addType('enum_yes_no', \YesNoEnumType::class);

You can now use enum_yes_no type.

/**
 * @Column(type="enum_yes_no", nullable=true)
 * @var \YesNoEnum|null
 */
private $yesNo;

For more info on the custom Doctrine mapping types, check official documentation.

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.

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.

统计信息

  • 总下载量: 258
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固