symfony - What do you use instead of ENUM in doctrine2

Mots clés : symfonydoctrine-ormsymfony

meilleur 4 Réponses symfony - What do you use instead of ENUM in doctrine2

vote vote

96

class MyEntity {     const STATUS_INACTIVE = 0;     const STATUS_ACTIVE = 1;     const STATUS_REFUSE = 2;      protected $status = self::STATUS_ACTIVE; } 
vote vote

90

CREATE TYPE new_enum AS ENUM ('sad', 'ok', 'happy'); 
@ORM\Column(name="name", type="string", columnDefinition="new_enum",  nullable=true) 
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 # <======= 
vote vote

72

<?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;     } } 
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,      ];  } 
doctrine:     dbal:         types:             UploadFileStatusType: App\Enum\UploadFileStatusType 
class MyEntity {     /**      * @var string      *      * @ORM\Column(type="UploadFileStatusType")      */     protected $status; } 
enum('open', 'done', 'error') 
vote vote

64

<?php namespace App\DBAL\Types;  use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;  final class BasketballPositionType extends AbstractEnumType {     public const POINT_GUARD = 'PG';     public const SHOOTING_GUARD = 'SG';     public const SMALL_FORWARD = 'SF';     public const POWER_FORWARD = 'PF';     public const CENTER = 'C';      protected static $choices = [         self::POINT_GUARD => 'Point Guard',         self::SHOOTING_GUARD => 'Shooting Guard',         self::SMALL_FORWARD => 'Small Forward',         self::POWER_FORWARD => 'Power Forward',         self::CENTER => 'Center'     ]; } 
doctrine:     dbal:         types:             BasketballPositionType: App\DBAL\Types\BasketballPositionType 
<?php namespace App\Entity;  use App\DBAL\Types\BasketballPositionType; use Doctrine\ORM\Mapping as ORM; use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;  /**  * @ORM\Entity()  * @ORM\Table(name="players")  */ class Player {     /**      * @ORM\Id      * @ORM\Column(name="id", type="integer")      * @ORM\GeneratedValue(strategy="AUTO")      */     protected $id;      /**      * Note, that type of a field should be same as you set in Doctrine config      * (in this case it is BasketballPositionType)      *      * @ORM\Column(name="position", type="BasketballPositionType", nullable=false)      * @DoctrineAssert\Enum(entity="App\DBAL\Types\BasketballPositionType")           */     protected $position;      public function getId()     {         return $this->id;     }      public function setPosition(string $position)     {         $this->position = $position;     }      public function getPosition(): string     {         return $this->position;     } } 
$player->setPosition(BasketballPositionType::POINT_GUARD); 

Questions similaires