DoctrineEnumBundle icon indicating copy to clipboard operation
DoctrineEnumBundle copied to clipboard

Issues with types naming in YAML configuration after upgrade to v9.0

Open mzurawek opened this issue 2 years ago • 2 comments

When upgrading bundle (v8.1.2 -> v9.0.0) I had to change configuration in doctrine.yaml.

Before:

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

This produced error: Unknown type BasketballPositionType

What finally worked (full class path instead of just class name):

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

How entity looks like:

#[ORM\Entity(repositoryClass: FooRepository::class)]
class Foo
{
    #[ORM\Column(type: BasketballPositionType::class, nullable: false)]
    #[DoctrineAssert\EnumType(entity: BasketballPositionType::class)]
    private string $basketballPositionType;
}

mzurawek avatar Jun 14 '22 14:06 mzurawek

@mzurawek I tried to reproduce, but couldn't. Please check the comment for this column in DB. Should be something like this '(DC2Type:BasketballPositionType)'

fre5h avatar Jun 14 '22 15:06 fre5h

Yes, there is the right comment.

I've done the clean install of Symfony 6.1, done configuration according to: https://github.com/fre5h/DoctrineEnumBundle/blob/main/Resources/docs/usage_example.md And even when starting php bin/console make:migration I've got: In Exception.php line 120:

Unknown column type "App\DBAL\Types\BasketballPositionType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection t  
  hen you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

Changing doctrine.yaml configuration to:

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

solves the issue, but the comment on the field generated by migration is: (DC2Type:App\DBAL\Types\BasketballPositionType) - I assume it's not the desired solution.

Changing entity property attribute to pure class name (not path) also solves the problem: #[ORM\Column(type: "BasketballPositionType", nullable: false)] So maybe it's only a problem with documentation?

I can provide you with this clean starting template but I'm not sure how to do that here. I'm running PHP 8.1.7

Edit: Finally I've changed type name in entity attribute to just class name (not full class path) and it works as before upgrading, so I assume it was just and error in documentation to use BasketballPositionType::class.

mzurawek avatar Jun 14 '22 19:06 mzurawek

I have the same problem: Using #[ORM\Column(type: BasketballPositionType::class, nullable: false)] as documentation says, it does not work. Using #[ORM\Column(type: "BasketballPositionType", nullable: false)] works.

v9.0.3

dehy avatar Aug 27 '22 09:08 dehy

Found the cause. The error was in docs example. I've updated the doc. So now the correct syntax is

doctrine:
  dbal:
    types:
      BasketballPositionType: App\DBAL\Types\BasketballPositionType
#[ORM\Column(type: 'BasketballPositionType')]
#[DoctrineAssert\EnumType(entity: BasketballPositionType::class)]
private $position;

Alternatively you can use next:

doctrine:
  dbal:
    types:
      App\DBAL\Types\BasketballPositionType: App\DBAL\Types\BasketballPositionType
#[ORM\Column(type: BasketballPositionType::class)]
#[DoctrineAssert\EnumType(entity: BasketballPositionType::class)]
private $position;

fre5h avatar Aug 27 '22 11:08 fre5h

In future I plan to add autoregistration of enum types. So it is not be required to add custom enum types into doctrine.dbal.types section. But it is not implemented yet. So for now I left two variants of configuration.

fre5h avatar Aug 27 '22 11:08 fre5h