Skip to content

Latest commit

 

History

History
116 lines (91 loc) · 3.12 KB

usage_example.md

File metadata and controls

116 lines (91 loc) · 3.12 KB

Usage example

This example will show how to create a custom ENUM field for basketball positions. This ENUM should contain five values:

  • PG - Point Guard
  • SG - Shooting Guard
  • SF - Small Forward
  • PF - Power Forward
  • C - Center

Create a class for a new ENUM type BasketballPositionType:

<?php
namespace App\DBAL\Types;

use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;

/**
 * @extends AbstractEnumType<string, string>
 */
final class BasketballPositionType extends AbstractEnumType
{
    public final const POINT_GUARD = 'PG';
    public final const SHOOTING_GUARD = 'SG';
    public final const SMALL_FORWARD = 'SF';
    public final const POWER_FORWARD = 'PF';
    public final const CENTER = 'C';

    protected static array $choices = [
        self::POINT_GUARD => '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.yaml:

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

Create a Player entity that has a position field:

<?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(type: 'integer', name: 'id')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private $id;

    // Note, that type of field should be same as you set in Doctrine config (in this case it is BasketballPositionType)
    #[ORM\Column(type: 'BasketballPositionType')]
    #[DoctrineAssert\EnumType(entity: BasketballPositionType::class)]
    private $position;

    public function getId()
    {
        return $this->id;
    }

    public function setPosition(string $position)
    {
        BasketballPositionType::assertValidChoice($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);

But don't forget to define BasketballPositionType in the use section:

use App\DBAL\Types\BasketballPositionType;

More features