Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 2.42 KB

hook_for_doctrine_migrations.md

File metadata and controls

53 lines (36 loc) · 2.42 KB

Hook for Doctrine migrations

If you use Doctrine migrations in your project you should be able to create migrations for you custom ENUM types. If you want to create migration for the new ENUM type, then just use console commands doctrine:migrations:diff to create migration and doctrine:migrations:migrate to execute it.

For the previous example of BasketballPositionType for MySQL DB (e.g.) Doctrine will generate SQL statement, that looks like this:

CREATE TABLE players (
    id INT AUTO_INCREMENT NOT NULL,
    position ENUM('PG', 'SG', 'SF', 'PF', 'C') NOT NULL COMMENT '(DC2Type:BasketballPositionType)',
    PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB

You can see here the comment '(DC2Type:BasketballPositionType)' for position column. Doctrine will know that this column should be processed as BasketballPositionType.

If you later need to add new values to ENUM or delete some existing, you also need to create new migrations. But Doctrine won't detect any changes in your ENUM... :(

Fortunately you can do simple hook =) Access your database and delete comment for position column. Then run console command doctrine:migrations:diff it will create correct migrations.

You should repeat these steps after each update of your custom ENUM type!

Console command for dropping comments

This bundle provides a special console command doctrine:enum:drop-comment that can help you easily drop comments for selected ENUM type. So you don't need to do this manually in DB.

$ bin/console doctrine:enum:drop-comment BasketballPositionType

Dropping comments for BasketballPositionType type...
==========================================

  * App\Entity\Player::$position   Dropped

 TOTAL: 1

 [OK] DONE   

More features