我曾经处理过一个类似的问题,需要将MSSQL实例从utf8转换为latin1或者反过来。我采用了以下步骤:
- 定义一个自定义的DBAL类型
- 在启动Bundle方法中进行初始化(我认为也可以在
config.yml
中进行)
- 在实体字段上使用它作为注释
以下是代码:
DBAL CUSTOM TYPE
<?php
namespace Acme\DemoBundle\Doctrine\Type;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Utf8String extends StringType
{
public function convertToDatabaseValue($value, AbstractPlatform $p)
{
return mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
}
public function convertToPHPValue($value, AbstractPlatform $p)
{
return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
}
}
捆绑初始化
<?php
namespace Acme\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\DBAL\Types\Type;
class AcmeDemoBundle extends Bundle
{
public function boot() {
if (!Type::hasType('utf8string'))
{
Type::addType('utf8string', 'Acme\DemoBundle\Doctrine\Type\Utf8String');
$em = $this->container->get('doctrine.orm.em_my_sqlserver_entity_manager');
$conn = $em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('Utf8String', 'utf8string');
}
}
}
一个映射实体的示例
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Vehicle
{
private $name;
private $idVehicle;
....
}
希望这能够帮到你,并且希望你也能找到更好的解决方案!