2015-10-18 68 views
0

我有一個應用程序,我在嘗試Doctrine2。我有一個數據庫配置了類型InnoDB和排序規則utf8_unicode_ci.I然後有一個簡單的表,有兩列:id(integer)和name(string)。Utf8,瑞典語字符,Doctrine,Mysql和Json序列化

我用phpmyadmin進入行插入表,然後我用下面的代碼從數據庫中讀取的信息:

$serializer = JMS\Serializer\SerializerBuilder::create()->build(); 
$county = $entityManager->find('County', 1); 
$jsonContent = $serializer->serialize($county, 'json'); 

我使用的串行爲http://jmsyst.com/libs/serializer

我的實體文件爲縣是這樣的:

<?php 
use Doctrine\ORM\Mapping as ORM; 

// src/County.php 
/** @ORM\Entity **/ 
class County 
{ 
    /** @ORM\Id @ORM\Column(type="integer") **/ 
    protected $id; 

    /** @ORM\Column(type="string") * */ 
    protected $name; 


    public function getId() 
    { 
     return $this->id; 
    } 

    public function setId($id) 
    { 
     $this->id = $id; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 
} 

當我試圖在沒有瑞典字符一切正常連續運行它,但如果我通過phpmyadmin進入瑞典字符「a」並嘗試讀取該行,我得到以下異常:

Fatal error: Uncaught exception 'RuntimeException' with message 'Your data could not be encoded because it contains invalid UTF8 characters.' in C:\wamp\www\doctrine\vendor\jms\serializer\src\JMS\Serializer\JsonSerializationVisitor.php on line 36

任何提示我做錯了什麼?我認爲這應該工作。

回答

0

我想通了。我將charset添加到連接對象,然後工作。

$conn = array(
'driver' => 'pdo_mysql', 
'dbname' => 'doctrine', 
'user' => 'doctrine', 
'password' => 'doctrine', 
'host' => 'localhost', 
'charset' => 'UTF8' 
);