You could create your own custom "string" type and override it's class in Doctrine\DBAL\Types\Type
. Type::convertToDatabaseValue
and Type::convertToPHPValue
would be what you want to override.
<?php
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Utf8StringType extends StringType
{
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $p)
{
// convert from utf8 to latin1
return mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $p)
{
// convert from latin1 to utf8
return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
}
}
Then put the type into Doctrine with a new name or replace the string type:
<?php
\Doctrine\DBAL\Types\Type::addType('utf8string', 'Utf8StringType');
// replace the default string type
\Doctrine\DBAL\Types\Type::overrideType('string', 'Utf8StringType');