Sorting Doctrine migration tables
Trying to find a way to programmatically create tables with a specific collation, but can't find a way to do it correctly. I am using "doctrine / doctrine-migrations-bundle": "2.1. * @Dev" and Symfony 2.3,
I have set in my config.yml:
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: LATIN1
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
doctrine_migrations:
dir_name: %kernel.root_dir%/../src/CF/EscritorioBundle/Migrations
namespace: MyNameSpace\Migrations
table_name: migrations
name: Application Migrations
It creates a database with LATIN1 and latin1_swedish_ci encoding by default when I run the tool doctrine:database:create
. Then I run my migrations and all tablesutf8_general_ci
Looked at the function $schema->createTable()
but couldn't find a way to pass the mapping I needed. What's the pertinent workaround here?
source to share
Well, this is kind of ugly, but the only way I've found is to create after all the tables a different set of migrations that changed the table and converted the encoding. migration functions up()
and down()
looked like this:
......
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql("ALTER TABLE MyTable CONVERT TO CHARACTER SET LATIN1 COLLATE latin1_general_ci");
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql("ALTER TABLE MyTable CONVERT TO CHARACTER SET UTF8 COLLATE utf8_general_ci");
}
source to share
you can set the default connection option_table_options to achieve this: in symfony this is done via:
doctrine:
dbal:
default_table_options:
charset: latin1
collate: latin1_general_ci
for those looking to do it in simple doctrine, this translates to the defaultDatabaseOptions doctrine connect option and is passed to the entity manager along with your database credentials, etc .:
[
...
'driver' => ...
'user' => ...
...
'defaultDatabaseOptions' => [
'charset' => 'latin1',
'collate' => 'latin1_general_ci'
]
]
source to share