What do you use instead of ENUM in doctrine2?

后端 未结 4 1750
半阙折子戏
半阙折子戏 2021-01-30 13:04

What do you use instead of ENUM in Doctrine2? smallint? I thought of using varchar, or explicitly define char, but this may not be very effective when it comes to indexes, or am

4条回答
  •  孤独总比滥情好
    2021-01-30 13:41

    You should use fre5h/DoctrineEnumBundle for doctrine when using symfony:

    Example of using

    Create a class for new ENUM type BasketballPositionType:

     'Point Guard',
            self::SHOOTING_GUARD => 'Shooting Guard',
            self::SMALL_FORWARD => 'Small Forward',
            self::POWER_FORWARD => 'Power Forward',
            self::CENTER => 'Center'
        ];
    }
    

    Register BasketballPositionType for Doctrine in config.yml:

    doctrine:
        dbal:
            types:
                BasketballPositionType: App\DBAL\Types\BasketballPositionType
    

    Create a Player entity that has a position field:

    id;
        }
    
        public function setPosition(string $position)
        {
            $this->position = $position;
        }
    
        public function getPosition(): string
        {
            return $this->position;
        }
    }
    

    Now you can set a position for Player inside some action or somewhere else:

    $player->setPosition(BasketballPositionType::POINT_GUARD);
    

提交回复
热议问题