Search code examples
phpmysqlsymfonydoctrine-ormdoctrine

Where should I register my DBAL type?


I am using Doctrine's enum types to track the status of an entity that I am using in a Symfony application. I am using (roughly) the methods described here:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html

My problem comes when I try to update the database schema. I get the following error:

  [Doctrine\DBAL\DBALException]
  Unknown column type "EnumStatusType" 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 \Doctrin
  e\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might
   have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctr
  ineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type nam
  e is empty you might have a problem with the cache or forgot some mapping information.

This error is very helpful in a way -- as is the documentation -- but both of those resources leave out two pieces of information: In which file should I use addType() to register my new type?

Two secondary questions: Should I call the addType() method statically, as shown in the examples? If not, how should I retrieve an object in order to call the method non-statically?


Solution

  • You can use the symfony configuration to add your custom types.

    If you are using symfony 4+ you can into config/packages/doctrine.yaml this code:

    doctrine:
        dbal:
            types:
                your_custom_type:  App\Type\YourCustomType
    

    If you are using symfony 3.x you can add into this file app/config/config.yml this code:

    doctrine:
        dbal:
            types:
                your_custom_type:  AppBundle\Type\YourCustomType