【问题标题】:Twig not showing utf8 characters with doctrine object (SQL server)Twig 不显示带有教义对象的 utf8 字符(SQL 服务器)
【发布时间】:2014-06-17 15:05:02
【问题描述】:

我在渲染树枝模板时无法显示某些字符。我从 SQL Server 获取数据

    $em = $this->getDoctrine()->getManager();
$newsData = $em->getRepository("EstadisticasBundle:Novedades")->findAll();
return $this->render("EstadisticasBundle:index:index.html.twig",array("news" => $newsData));

当我在我的树枝上使用它时,我会这样做

    {% set i = 0 %}
    {% for i in 0.. news|length-1 %}
    {% set novedad = news[i] %}
{{ novedad.detalle }}
    {% endfor %} 

而且它不显示特殊字符。如果我使用 |raw,它会显示如下:“2012 Veinte a�os”。

当我将 die() 用于打印或在我的树枝上写特殊字符时,我在显示字符时没有任何问题。它只发生在作为查询结果的对象的数组上

【问题讨论】:

    标签: php sql symfony doctrine-orm


    【解决方案1】:

    这已修复,我认为我的 DB 字符集是 UTF8 但它不是(因为我没有创建这个 DB)。我用

    修复了它
        {{ novedad.detalle|convert_encoding('UTF-8', 'ISO-8859-1') }} 
    

    【讨论】:

      【解决方案2】:

      在 Symfony2 config.yml 中为 Sql Server 驱动程序定义字符集时要小心。

      对于pdo_mysql驱动的字符集参数是:

      字符集:UTF8

      但是对于 sqlsrv 驱动的字符集是:

      字符集:UTF-8

      注意并记住输入连字符“-”

      doctrine:
          dbal:
              default_connection: default
              connections:
                default:
                  driver:   sqlsrv
                  host:     %database_host%
                  port:     1433
                  dbname:   yourdatabasename
                  user:     %database_user%
                  password: %database_password%
                  charset:  UTF-8
      

      【讨论】:

        猜你喜欢
        • 2018-04-16
        • 2014-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多