9

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?

Rui Gonçalves
  • 2,423
  • 6
  • 30
  • 42

7 Answers7

20
  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.

Community
  • 1
  • 1
gpilotino
  • 13,055
  • 9
  • 48
  • 61
5

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,
                   ));
pfp
  • 51
  • 1
  • 1
1

Shortcut:

symfony doctrine:build --all-classes --and-migrate
gpupo
  • 942
  • 9
  • 16
0

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'
                )
            )
        );
}
falsch
  • 1,425
  • 2
  • 14
  • 21
0

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

ken
  • 4,886
  • 4
  • 30
  • 26
0

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');
    ...
}
coderich
  • 83
  • 5
-1
Model:  
  column:  
    type: enum  
    values: [one, two, three]  

(optional:)  
    notnull: false  
    default: one #or two or three  
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79