How to change column charset and collation with Doctrine?

Solution 1:

Short answer

Doctrine won't do this for you.

Alternative solution

Using DoctrineMigrationBundle allows to create migrations in plain SQL. Thus, you can edit auto-generated migrations to add charset/collation on desired columns.

Generate a new blank migration:

bin/console doctrine:migrations:generate

Then, add the ALTER statement:

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20200113131732 extends AbstractMigration {
    public function up(Schema $schema) : void {
        $this->addSql('ALTER TABLE categories MODIFY `description` VARCHAR(191) COLLATE utf8mb4_unicode_ci NOT NULL;');
    }

    public function down(Schema $schema) : void {
        $this->addSql('ALTER TABLE categories MODIFY `description` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL;');        
    }
}

Finally, just run the migration:

bin/console doctrine:migrations:migrate

Solution 2:

It looks like doctrine partialy supports the change of collation on columns (only supported by Drizzle, Mysql, PostgreSQL>=9.1, Sqlite and SQLServer).

See doctrine annotations reference

    /**
     * @ORM\Table(name="categories")
     */
    class Category {
        // ...
    
        /**
         * @var string
         * @ORM\Column(name="description", type="string", length=255, options={"collation"="ascii_general_ci"})
         */
        private $description;
    }