Symfony2 + Doctrine:如何将iso8859-1转换为utf-8,反之亦然? [英] Symfony2+Doctrine: How to convert iso8859-1 to utf-8 and viceversa?
问题描述
有没有办法转换所有数据从数据库接收到utf8,并将所有数据发送到DB到iso-8859-1,而不是将网站转换为iso8859-1?也许使用Doctrine事件订阅者?如果是这样,我应该拦截哪些事件?
我解决了一个类似的问题,用于从utf8转换为latin1的MSSQL实例,反之亦然。我执行以下步骤:
- 定义自定义DBAL类型
- 启动Bundle方法的初始化(我想你可以在
config.yml
中执行) - 在实体字段上使用它作为注释
他的代码如下:
DBAL CUSTOM TYPE p>
<?php
命名空间Acme\DemoBundle\Doctrine\Type;
使用Doctrine\DBAL\Types\StringType;
使用Doctrine\DBAL\Platforms\AbstractPlatform;
class Utf8String extends StringType
{
/ **
* {@inheritdoc}
* /
public function convertToDatabaseValue($ value, AbstractPlatform $ p)
{
//从utf8转换为latin1
返回mb_convert_encoding($ value,'ISO-8859-1','UTF-8');
}
/ **
* {@inheritdoc}
* /
public function convertToPHPValue($ value,AbstractPlatform $ p)
{
//从latin1转换为utf8
返回mb_convert_encoding($ value,'UTF-8','ISO-8859-1');
}
}
BUNDLE INITIALISATION p>
<?php
命名空间Acme\DemoBundle;
使用Symfony\Component\HttpKernel\Bundle\Bundle;
使用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
命名空间Acme\DemoBundle \实体;
使用Doctrine\ORM\Mapping作为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整数
*
* @ ORM\Column(name =ID_VEHICLE,type =integer ,nullable = true)
* /
private $ idVehicle;
....
}
希望这个帮助和希望你找到更好的解决方案!!!
We're building a Symfony2 app that's using an Oracle database. All the data in the DB is encoded as WE8ISO8859P15 (iso-8859-1), and all the website is encoded as utf-8.
Is there any way to convert all the data received from the database to utf8, and all the data sent to the DB to iso-8859-1, instead of convert the website to iso8859-1? Maybe using a Doctrine event subscriber? If so, which events should I intercept?
I solve a similar problem with a MSSQL instance for convert from utf8 to latin1 and viceversa. I do the following step:
- Define a custom DBAL Type
- Initialise on the boot Bundle method (i think you can do in the
config.yml
too) - Use it as annotation on the entity field
This his the code:
DBAL CUSTOM TYPE
<?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');
}
}
BUNDLE INITIALISATION
<?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');
}
}
}
AN EXAMPLE MAPPED ENTITY
<?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;
....
}
Hope this help and hoping you find a better solutions too!!!
这篇关于Symfony2 + Doctrine:如何将iso8859-1转换为utf-8,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!