在Symfony4

时间:2018-04-15 08:18:22

标签: php symfony doctrine-orm doctrine symfony4

所以我有这个自定义Doctrine类型

命名空间App \ Doctrine \ Types;

使用Doctrine \ DBAL \ Platforms \ AbstractPlatform; 使用Doctrine \ DBAL \ Types \ TextType;

class MyType extends TextType
{
   private $prefix='';

   public function getName()
   {
      return 'my_type';
   }
   public function setPrefix(string $prefix)
   {
      $this->prefix=$prefix;
   }
}

我在config / packages / doctrine.yml中注册:

doctrine:
    dbal:
        types:
            my_type: App\Doctrine\Types\MyType

然后在内核启动()中我试图为这种类型添加一些参数:

public function boot() {
   parent::boot();

   $myType=Type::getType('my_type');
   $myType->setPrefix('abc');
}

第一次运行应用程序时,这非常有效。前缀是为类型设置的,可以在整个应用程序中使用。但是,我第二次得到例外:

  

未知列类型" encrypted_text"请求。任何学说   您使用的类型必须注册   \原则\ DBAL \类型\型号::的addType()。你可以得到所有的清单   已知类型为\ Doctrine \ DBAL \ Types \ Type :: getTypesMap()。如果这   数据库内省期间可能会出现错误   忘记注册Doctrine Type的所有数据库类型。使用   AbstractPlatform #registerDoctrineTypeMapping()或您的自定义   types实现Type#getMappedDatabaseTypes()。如果类型名称是   空的你可能有缓存问题或忘记了一些映射   信息。

然后我将boot()更改为:

    public function boot() {
       parent::boot();
       if (!Type::hasType('my_type')) {
           Type::addType('my_type', 'App\Doctrine\Types\MyType');
       }
       $myType=Type::getType('my_type');
       $myType->setPrefix('abc');
   }

现在异常消失了,但未设置前缀。我知道这些例外情况为我提供了有关该做什么的信息,但我真的不知道从哪里开始。

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:0)

现在我通过从config / packages / doctrine.yml中删除它来修复它,所以它不再在那里注册了。在内核中,我现在可以加载它:

    public function boot() {
       parent::boot();
       if (!Type::hasType('my_type')) {
           Type::addType('my_type', 'App\Doctrine\Types\MyType');
       }
       $myType=Type::getType('my_type');
       $myType->setPrefix('abc');
   }

在构建缓存之前,我仍然无法理解为什么会这样,但是一旦构建了缓存,我就无法理解。但是,我现在可以继续。

如果有人有更好的答案,我会非常乐意接受它。

答案 1 :(得分:0)

#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.**

要在DB表中作为枚举键入自定义和映射,您需要具有更高版本的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.

    #This is your Base class type.

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;

/**
 * Players
 *
 * @ORM\Table(name="players")
 * @ORM\Entity
 */
class Players
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="position", type="BasketballPositionType", length=255, nullable=false)
     * @DoctrineAssert\Enum(entity="App\DBAL\Types\BasketballPositionType")
     */
    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