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?
modify your schema
run
./symfony doc:generate-migrations-diff
this will generate one or more files in lib/migrations/doctrine/
run
./symfony doc:migrate
this will apply the generated migrations to the database
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.
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,
));
Shortcut:
symfony doctrine:build --all-classes --and-migrate
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
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');
...
}
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'
)
)
);
}
Model:
column:
type: enum
values: [one, two, three]
(optional:)
notnull: false
default: one #or two or three
精彩评论