Вы должны использовать fre5h / DoctrineEnumBundle для доктрины при использовании symfony:
Пример использования
Создать класс для нового типа ENUM BasketballPositionType:
<?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'
];
}
Зарегистрируйте BasketballPositionType для Doctrine в config.yml:
doctrine:
dbal:
types:
BasketballPositionType: App\DBAL\Types\BasketballPositionType
Создать объект Player, имеющий поле позиции:
<?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);