enum not supported #5599

Closed
opened 2026-01-22 15:12:24 +01:00 by admin · 4 comments
Owner

Originally created by @wsalazar on GitHub (Jul 5, 2017).

Good afternoon,

I have an issue with the mysql enum type. My company uses a framework that is no longer supported, Kohana 3.x. We have a schema that uses the enum type. I've read some documentation that says that Doctrine2 doesn't support enum because enum has state while Doctrine2 does not. That is to say, enum comes predefined with values already and Doctrine doesn't like that.

This is because Enums contain state (their allowed values) and Doctrine types don’t.

This quote can be found in the following link that I provided below. Last sentence 2nd paragraph.

So what I had to do is create my own type.
I followed this:
Doctrine Documentation for Enums. I created the generic EnumType class. I then created my Enum type called Enumscheduletype with the following code:

<?php defined('SYSPATH') or die('No direct access allowed.');

class DBAL_Enumscheduletype extends DBAL_EnumType
{
    protected $name = 'enumschedule';
    protected $values = array('daily','weekly','monthly');
}

In my entity this is how I'm mapping my enum type field.

/**
     * @Column(type="enumschedule", name="frequency", options={"default":"weekly"})
     */
    public $frequency;

However, when I ran vendor/bin/doctrine orm:schema-tool:update --dump-sql.....

I got the following error.

[Doctrine\DBAL\DBALException]                                                                    
  Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

I then realized that in a table that I have in the db I have an enum type. Doctrine didn't like reading it.
So I found out where:

/**
     * Gets the Doctrine type that is mapped for the given database column type.
     *
     * @param string $dbType
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException
     */
    public function getDoctrineTypeMapping($dbType)
    {
        if ($this->doctrineTypeMapping === null) {
            $this->initializeAllDoctrineTypeMappings();
        }

        $dbType = strtolower($dbType);
/******************Piece of code that I added***************************/

        if($dbType == 'enum') {
            $dbType = 'enumschedule';
        }
/******************Piece of code that I added***************************/
        if (!isset($this->doctrineTypeMapping[$dbType])) {
            throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it.");
        }

        return $this->doctrineTypeMapping[$dbType];
    }

I had to hack Doctrine so that it would recognize my type. Notice the "Piece of code that I added in the code above. This is in the file: vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php. Problem is, I should not have to hack Doctrine. Is there another way around this? How can I hook into Doctrine so that upon reading my schema it'll recognize my type and not what it reads from the db.

Originally created by @wsalazar on GitHub (Jul 5, 2017). Good afternoon, I have an issue with the mysql enum type. My company uses a framework that is no longer supported, Kohana 3.x. We have a schema that uses the enum type. I've read some documentation that says that Doctrine2 doesn't support enum because enum has state while Doctrine2 does not. That is to say, enum comes predefined with values already and Doctrine doesn't like that. > This is because Enums contain state (their allowed values) and Doctrine types don’t. This quote can be found in the following link that I provided below. Last sentence 2nd paragraph. So what I had to do is create my own type. I followed this: [Doctrine Documentation for Enums](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html). I created the generic EnumType class. I then created my Enum type called Enumscheduletype with the following code: ``` <?php defined('SYSPATH') or die('No direct access allowed.'); class DBAL_Enumscheduletype extends DBAL_EnumType { protected $name = 'enumschedule'; protected $values = array('daily','weekly','monthly'); } ``` In my entity this is how I'm mapping my enum type field. ``` /** * @Column(type="enumschedule", name="frequency", options={"default":"weekly"}) */ public $frequency; ``` However, when I ran vendor/bin/doctrine orm:schema-tool:update --dump-sql..... I got the following error. ``` [Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it. ``` I then realized that in a table that I have in the db I have an enum type. Doctrine didn't like reading it. So I found out where: ``` /** * Gets the Doctrine type that is mapped for the given database column type. * * @param string $dbType * * @return string * * @throws \Doctrine\DBAL\DBALException */ public function getDoctrineTypeMapping($dbType) { if ($this->doctrineTypeMapping === null) { $this->initializeAllDoctrineTypeMappings(); } $dbType = strtolower($dbType); /******************Piece of code that I added***************************/ if($dbType == 'enum') { $dbType = 'enumschedule'; } /******************Piece of code that I added***************************/ if (!isset($this->doctrineTypeMapping[$dbType])) { throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it."); } return $this->doctrineTypeMapping[$dbType]; } ``` I had to hack Doctrine so that it would recognize my type. Notice the "Piece of code that I added in the code above. This is in the file: vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php. Problem is, I should not have to hack Doctrine. Is there another way around this? How can I hook into Doctrine so that upon reading my schema it'll recognize my type and not what it reads from the db.
admin closed this issue 2026-01-22 15:12:25 +01:00
Author
Owner

@Ocramius commented on GitHub (Jul 5, 2017):

See:

https://stackoverflow.com/questions/8750724/what-do-you-use-instead-of-enum-in-doctrine2/9057352
*
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html#solution-1-mapping-to-varchars
(specifically, this is the simplest/cleanest solution)

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On Wed, Jul 5, 2017 at 10:46 PM, was notifications@github.com wrote:

Good afternoon,

I have an issue with the mysql enum type. My company uses a framework that
is no longer supported, Kohana 3.x. We have a schema that uses the enum
type. I've read some documentation that says that Doctrine2 doesn't support
enum because enum has state while Doctrine2 does not. That is to say, enum
comes predefined with values already and Doctrine doesn't like that.

This is because Enums contain state (their allowed values) and Doctrine
types don’t.
This quote can be found in the following link that I provided below. Last
sentence 2nd paragraph.

So what I had to do is create my own type.
I followed this:
Doctrine Documentation for Enums
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.htmlI
created the generic EnumType class. I then created my Enum type called
Enumscheduletype with the following code:

<?php defined('SYSPATH') or die('No direct access allowed.'); class DBAL_Enumscheduletype extends DBAL_EnumType { protected $name = 'enumschedule'; protected $values = array('daily','weekly','monthly'); } In my entity this is how I'm mapping my enum type field. /** * @Column(type="enumschedule", name="frequency", options={"default":"weekly"}) */ public $frequency; However, when I ran vendor/bin/doctrine orm:schema-tool:update --dump-sql..... I got the following error. [Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it. I then realized that in a table that I have in the db I have an enum type. Doctrine didn't like reading it. So I found out where: /** * Gets the Doctrine type that is mapped for the given database column type. * * @param string $dbType * * @return string * * @throws \Doctrine\DBAL\DBALException */ public function getDoctrineTypeMapping($dbType) { if ($this->doctrineTypeMapping === null) { $this->initializeAllDoctrineTypeMappings(); } $dbType = strtolower($dbType); if($dbType == 'enum') { $dbType = 'enumschedule'; } if (!isset($this->doctrineTypeMapping[$dbType])) { throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it."); } return $this->doctrineTypeMapping[$dbType]; } I had to hack Doctrine so that it would recognize my type. Problem is, I should not have to hack Doctrine. Is there another way around this? How can I hook into Doctrine so that upon reading my schema it'll recognize my type and not what it reads from the db. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub , or mute the thread .
@Ocramius commented on GitHub (Jul 5, 2017): See: * https://stackoverflow.com/questions/8750724/what-do-you-use-instead-of-enum-in-doctrine2/9057352 * http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html#solution-1-mapping-to-varchars (specifically, this is the simplest/cleanest solution) Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Wed, Jul 5, 2017 at 10:46 PM, was <notifications@github.com> wrote: > Good afternoon, > > I have an issue with the mysql enum type. My company uses a framework that > is no longer supported, Kohana 3.x. We have a schema that uses the enum > type. I've read some documentation that says that Doctrine2 doesn't support > enum because enum has state while Doctrine2 does not. That is to say, enum > comes predefined with values already and Doctrine doesn't like that. > > This is because Enums contain state (their allowed values) and Doctrine > types don’t. > This quote can be found in the following link that I provided below. Last > sentence 2nd paragraph. > > So what I had to do is create my own type. > I followed this: > Doctrine Documentation for Enums > <http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html>I > created the generic EnumType class. I then created my Enum type called > Enumscheduletype with the following code: > > <?php defined('SYSPATH') or die('No direct access allowed.'); > > class DBAL_Enumscheduletype extends DBAL_EnumType > { > protected $name = 'enumschedule'; > protected $values = array('daily','weekly','monthly'); > } > > In my entity this is how I'm mapping my enum type field. > > /** > * @Column(type="enumschedule", name="frequency", options={"default":"weekly"}) > */ > public $frequency; > > However, when I ran vendor/bin/doctrine orm:schema-tool:update > --dump-sql..... > > I got the following error. > > [Doctrine\DBAL\DBALException] > Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it. > > I then realized that in a table that I have in the db I have an enum type. > Doctrine didn't like reading it. > So I found out where: > > /** > * Gets the Doctrine type that is mapped for the given database column type. > * > * @param string $dbType > * > * @return string > * > * @throws \Doctrine\DBAL\DBALException > */ > public function getDoctrineTypeMapping($dbType) > { > if ($this->doctrineTypeMapping === null) { > $this->initializeAllDoctrineTypeMappings(); > } > > $dbType = strtolower($dbType); > if($dbType == 'enum') { > $dbType = 'enumschedule'; > } > if (!isset($this->doctrineTypeMapping[$dbType])) { > throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it."); > } > > return $this->doctrineTypeMapping[$dbType]; > } > > I had to hack Doctrine so that it would recognize my type. Problem is, I > should not have to hack Doctrine. Is there another way around this? How can > I hook into Doctrine so that upon reading my schema it'll recognize my type > and not what it reads from the db. > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6540>, or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakCwIBVG9ac46zltC39nqV12B-QRXks5sK_YXgaJpZM4OO2SE> > . >
Author
Owner

@wsalazar commented on GitHub (Jul 5, 2017):

Thanks. I tried that. Unfortunately, the columnDefinition and the options do not play nice together.

I tried this:

  /**
     * @Column(type="string", name="frequency", options={"default":"weekly"}, columnDefinition="ENUM('daily','weekly','monthly')", nullable=false)
     */
    public $frequency;

I get this when I do the --dump-sql:
ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly');

It should be:
ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly') default 'weekly' NOT NULL;

@wsalazar commented on GitHub (Jul 5, 2017): Thanks. I tried that. Unfortunately, the columnDefinition and the options do not play nice together. I tried this: ``` /** * @Column(type="string", name="frequency", options={"default":"weekly"}, columnDefinition="ENUM('daily','weekly','monthly')", nullable=false) */ public $frequency; ``` I get this when I do the --dump-sql: ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly'); It should be: ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly') default 'weekly' NOT NULL;
Author
Owner

@Ocramius commented on GitHub (Jul 5, 2017):

columnDefinition will ALWAYS fail at schema introspection, because you
are already in "custom stuff land".

This is perfectly OK: you should just use migrations anyway when
provisioning environments, which means that you will just have these pesky
diffs all over the place.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On Thu, Jul 6, 2017 at 12:52 AM, was notifications@github.com wrote:

Thanks. I tried that. Unfortunately, the columnDefinition and the options
do not play nice together.

I tried this:

/**
* @Column(type="string", name="frequency", options={"default":"weekly"}, columnDefinition="ENUM('daily','weekly','monthly')", nullable=false)
*/
public $frequency;

I get this when I do the --dump-sql:
ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly');

It should be:
ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly')
default 'weekly' NOT NULL;


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6540#issuecomment-313248438,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakCXt1Wc4Q8_zA4GO7qjJUTW4_BLyks5sLBOugaJpZM4OO2SE
.

@Ocramius commented on GitHub (Jul 5, 2017): `columnDefinition` will ALWAYS fail at schema introspection, because you are already in "custom stuff land". This is perfectly OK: you should just use migrations anyway when provisioning environments, which means that you will just have these pesky diffs all over the place. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Thu, Jul 6, 2017 at 12:52 AM, was <notifications@github.com> wrote: > Thanks. I tried that. Unfortunately, the columnDefinition and the options > do not play nice together. > > I tried this: > > /** > * @Column(type="string", name="frequency", options={"default":"weekly"}, columnDefinition="ENUM('daily','weekly','monthly')", nullable=false) > */ > public $frequency; > > I get this when I do the --dump-sql: > ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly'); > > It should be: > ALTER TABLE test_table ADD frequency ENUM('daily','weekly','monthly') > default 'weekly' NOT NULL; > > — > You are receiving this because you commented. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6540#issuecomment-313248438>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakCXt1Wc4Q8_zA4GO7qjJUTW4_BLyks5sLBOugaJpZM4OO2SE> > . >
Author
Owner

@beberlei commented on GitHub (Dec 6, 2020):

You need to register that $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'YourEnumType'); - however again here the statefullness is a problem, because what enum is meant? It only works when you use exactly one enum.

This is not something we want to support, as we don't use it ourselves internally. You can't benefit from all schema tool features when you have a legacy schema with enums.

@beberlei commented on GitHub (Dec 6, 2020): You need to register that `$connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'YourEnumType');` - however again here the statefullness is a problem, because what enum is meant? It only works when you use exactly one enum. This is not something we want to support, as we don't use it ourselves internally. You can't benefit from all schema tool features when you have a legacy schema with enums.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5599