#This is may answer!!!
**IMPORTANTE!! Because in Symfony/PHP (NO Object Oriented Language) don't storaging a state it's need make this form to mapping an ENUM type or any Custom Type similary to ENUM.**
如果您想在数据库表中自定义和映射类型,您需要使用 Symfony 的更高版本,Symfony >= 4.4 才能使用此 Bundle。请在您的项目中执行此命令(命令提示符)。
composer req fresh/doctrine-enum-bundle
Link example --> [https://github.com/fre5h/DoctrineEnumBundle][1] .
I made do this and it's ok for my app.
1
So, copy this class from this link
[https://github.com/fre5h/DoctrineEnumBundle/blob/master/DBAL/Types/AbstractEnumType.php][1]
and storage it in src/DBAL/Types =(IF NOT EXIST, CREATE IT), in your project Symfony => 4.4 o higher version.
2
Create this Table in Your Database (in my case I'm using MySql DB):
CREATE TABLE players (
id INT AUTO_INCREMENT NOT NULL,
position ENUM('PG', 'SG', 'SF', 'PF', 'C') NOT NULL,
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB
*****************
3
Create an Entity class for mapping table (Players) from your Databae inside your project level directory.
D:SYMFONY\app\myApp
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity --filter="Players"
4
Create getter and setters for your new Entity:
D:SYMFONY\app\myApp
php bin/console make:entity --regenerate App
5
Generate your CRUD operation for Entty class + frontend.
D:SYMFONY\app\myApp
php bin/console make:entity --regenerate App
6
Create your custom Type class 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'
];
}
7
Go inside this path in your Symfony project at
D:SYMFONY\app\myApp\config\packages
and create this class php : doctrime.php.
This class it's necesary for to Register your new Type.
Can you find more information at link for register new type in synfony
[https://symfony.com/doc/current/doctrine/dbal.html][1]
so - REGISTER YOUR NEW TYPE
Copy this fragment of code end paste in doctrime.php class:
<?php
$container->loadFromExtension('doctrine', [
'dbal' => [
'mapping_types' => [
'enum' => 'string', /////////mapping enum to as stringa
],
'types' => [
'BasketballPositionType' => App\DBAL\Types\BasketballPositionType::class,
],
],
]);
So, bindig ENUM type db in String and your new Type is "BasketballPositionType" !!!!
8
Now, use your Custom Type in your Entity Class mapping 'Players'.
<?php
namespace App\Entity;
** very important
use App\DBAL\Types\BasketballPositionType;
use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
use Doctrine\ORM\Mapping as ORM;
class Players
{
private $id;
private $position;
public function getId(): ?int
{
return $this->id;
}
public function getPosition(): ?string
{
return $this->position;
}
public function setPosition(string $position): self
{
$this->position = $position;
return $this;
}
}
运行
####################### RUM #######################
Run you Symfony embeded server
[ symfony serve ]
And go later in browser at https://127.0.0.1:8000/players
I'm use CMDER prompt command because it has Git incorporated.
It's very easy!!
[1]: https://symfony.com/doc/current/doctrine/dbal.html