Php Symfony2+;原则:如何将iso8859-1转换为utf-8,反之亦然?
我们正在构建一个使用Oracle数据库的Symfony2应用程序。数据库中的所有数据编码为WE8ISO8859P15(iso-8859-1),所有网站编码为utf-8Php Symfony2+;原则:如何将iso8859-1转换为utf-8,反之亦然?,php,symfony,encoding,utf-8,doctrine-orm,Php,Symfony,Encoding,Utf 8,Doctrine Orm,我们正在构建一个使用Oracle数据库的Symfony2应用程序。数据库中的所有数据编码为WE8ISO8859P15(iso-8859-1),所有网站编码为utf-8 是否有办法将从数据库接收到的所有数据转换为utf8,并将发送到DB的所有数据转换为iso-8859-1,而不是将网站转换为iso8859-1?也许使用条令事件订阅服务器?如果是,我应该拦截哪些事件?我解决了MSSQL实例从utf8转换为latin1的类似问题,反之亦然。我执行以下步骤: 定义自定义DBAL类型 初始化boot Bu
是否有办法将从数据库接收到的所有数据转换为utf8,并将发送到DB的所有数据转换为iso-8859-1,而不是将网站转换为iso8859-1?也许使用条令事件订阅服务器?如果是,我应该拦截哪些事件?我解决了MSSQL实例从utf8转换为latin1的类似问题,反之亦然。我执行以下步骤:
config.yml
中进行)<?php
namespace Acme\DemoBundle\Doctrine\Type;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Utf8String 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');
}
}
好主意!明天我要试试这个。非常感谢你!
<?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;
/**
*
* @ORM\Table(name="VEHICLE")
* @ORM\Entity()
*/
class Vehicle
{
/**
* @ORM\Id
* @ORM\Column(name="VEHICLE_NAME", type="utf8string", length=16, nullable=false)
*/
private $name;
/**
* @var integer
*
* @ORM\Column(name="ID_VEHICLE", type="integer", nullable=true)
*/
private $idVehicle;
....
}