Symfony2 + Doctrine:如何将iso8859-1转换为utf-8,反之亦然? [英] Symfony2+Doctrine: How to convert iso8859-1 to utf-8 and viceversa?

查看:114
本文介绍了Symfony2 + Doctrine:如何将iso8859-1转换为utf-8,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个使用Oracle数据库的Symfony2应用程序。数据库中的所有数据都编码为WE8ISO8859P15(iso-8859-1),所有网站都编码为utf-8。



有没有办法转换所有数据从数据库接收到utf8,并将所有数据发送到DB到iso-8859-1,而不是将网站转换为iso8859-1?也许使用Doctrine事件订阅者?如果是这样,我应该拦截哪些事件?

解决方案

我解决了一个类似的问题,用于从utf8转换为latin1的MSSQL实例,反之亦然。我执行以下步骤:


  1. 定义自定义DBAL类型

  2. 启动Bundle方法的初始化(我想你可以在 config.yml 中执行)

  3. 在实体字段上使用它作为注释

他的代码如下:



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:

  1. Define a custom DBAL Type
  2. Initialise on the boot Bundle method (i think you can do in the config.yml too)
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆