Rui Gonçalves
Rui Gonçalves

Reputation: 2433

Add new enum column during migration

Can anyone please tell me how can I add a new column of type enum to my schema in order to implement a Doctrine Migration?

Upvotes: 9

Views: 8883

Answers (7)

0x46616c6b
0x46616c6b

Reputation: 1475

I had the same problem and found a solution in set this flag in ProjectConfiguration.class.php

public function configureDoctrine(Doctrine_Manager $manager) {
    $manager->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
}

After that I used this method call and get a native mysql enum:

class MyMigration extends Doctrine_Migration_Base {
    public function up() {
        $this->changeColumn(self::tableName, 'columName', 'enum', null,
            array(
                'fixed' => true,
                'length' => null,
                'notnull' => true,
                'values' => array(
                    0 => 'Option 1',
                    1 => 'Option 2'
                )
            )
        );
}

Upvotes: 0

gpilotino
gpilotino

Reputation: 13312

  1. modify your schema

  2. run ./symfony doc:generate-migrations-diff

    this will generate one or more files in lib/migrations/doctrine/

  3. run ./symfony doc:migrate

    this will apply the generated migrations to the database

  4. run ./symfony doc:build --all-classes

    this works for symfony >= 1.3/1.4 and will rebuild all form/filters/model classes according to the modified schema

remember that the migration is generated comparing the new schema.yml to the current model classes, so if you rebuild your classes before running generate-migrations-diff you're screwed.

Upvotes: 20

coderich
coderich

Reputation: 83

The simplest way to run it from a Doctrine Migration is to register a new mapping. Then you can enforce values inside your entity if need be. (Doctrine MySQL Enums)

public function up(Schema $schema)
{
    $this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
    ...
}

Upvotes: 0

Jordan Warbelow-Feldstein
Jordan Warbelow-Feldstein

Reputation: 10748

Model:  
  column:  
    type: enum  
    values: [one, two, three]  

(optional:)  
    notnull: false  
    default: one #or two or three  

Upvotes: -1

pfp
pfp

Reputation: 51

In case you need to write the migration script yourself, here's an example of the syntax -- I haven't found a proper specification for the syntax anywhere.

$this->addColumn('tablename', 'column_name', 'enum', false, 
                  array('fixed' => 1,
                        'values' => 
                         array(0 => 'auto',
                               1 => 'manual',
                               2 => 'unknown'),
                         'default' => 'unknown',
                         'notnull' => true,
                         'length' => NULL,
                   ));

Upvotes: 5

gpupo
gpupo

Reputation: 982

Shortcut:

symfony doctrine:build --all-classes --and-migrate

Upvotes: 1

ken
ken

Reputation: 5086

Modify your schema and don't build yet the model. run doctrine schema diff then a migration class will be generated for you. Finally you can rebuild your models/forms/filters

Upvotes: 0

Related Questions