在doctrine2中你用什么而不是ENUM?

Dav*_*idW 37 symfony doctrine-orm

在Doctrine2中你用什么而不是ENUM?SMALLINT?我想过使用varchar,或者明确定义char,但是当涉及到索引时,这可能不是很有效,或者我错了?

Ocr*_*ius 68

我通常使用映射到类常量的整数,比如

class MyEntity {
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    const STATUS_REFUSE = 2;

    protected $status = self::STATUS_ACTIVE;
}
Run Code Online (Sandbox Code Playgroud)

这非常好用,可以更轻松地使用IDE中的ENUMS.

您还可以使用文档中描述的可枚举类型,但这意味着您必须为每个枚举列定义一个自定义类型.这是很多工作,没有真正的好处.

您可能还想知道为什么不应该使用枚举.

  • @Dennis的用法是`$ entity-> setStatus(MyEntity :: STATUS_ACTIVE);` (14认同)

web*_*sky 8

Postgres,symfony,orm,doctrine ......

  1. Postgress定义新类型枚举(pgAdmin)

CREATE TYPE new_enum AS ENUM('sad','ok','happy');

  1. 在实体中

@ORM\Column(name ="name",type ="string",columnDefinition ="new_enum",nullable = true)

  1. 在config.yml中

mapping_types:
    new_enum:string

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        mapping_types:
            new_enum: string # <=======
Run Code Online (Sandbox Code Playgroud)


Seb*_*eck 7

如果您希望使用 MySQL 和 Symfony 进行 MySQL ENUM,您现在可以使用一种简单的方法来实现,无需任何依赖

对所有枚举使用基类:

<?php

namespace App\Enum;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

abstract class EnumType extends Type
{
    protected $name;
    protected $values = [];

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $values = array_map(function($val) { return "'".$val."'"; }, $this->values);

        return "ENUM(".implode(", ", $values).")";
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (!in_array($value, $this->values)) {
            throw new \InvalidArgumentException("Invalid '".$this->name."' value.");
        }
        return $value;
    }

    public function getName()
    {
        return $this->name;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

针对您的特殊情况扩展它:

namespace App\Enum;

 class UploadFileStatusType extends EnumType
{
     const OPEN = 'open';
     const DONE = 'done';
     const ERROR = 'error';

     protected $name = self::class;

     protected $values = [
         self::OPEN => self::OPEN ,
         self::DONE => self::DONE,
         self::ERROR => self::ERROR,
     ];

}
Run Code Online (Sandbox Code Playgroud)

将其添加到您的 dcotrine.yml 配置文件中:

doctrine:
    dbal:
        types:
            UploadFileStatusType: App\Enum\UploadFileStatusType
Run Code Online (Sandbox Code Playgroud)

在您的实体文件中将其用作学说列文档类型:

class MyEntity
{
    /**
     * @var string
     *
     * @ORM\Column(type="UploadFileStatusType")
     */
    protected $status;
}
Run Code Online (Sandbox Code Playgroud)

最后你将得到一个干净的 MySQL ENUM 类型来表示列状态:

enum('open', 'done', 'error')
Run Code Online (Sandbox Code Playgroud)