2011-03-23 20 views
3

嘿everybod在那裏:) 我使用CodeIgniter和Doctrine2作爲我的ORM。 我遇到了從我的數據庫中存儲和檢索UTF-8文本的問題。Doctrine2和UTF-8

我有紅色的東西約PostConnection活動,但我不是很確定在哪裏添加這些東西:/

我的數據庫和表有UTF-8編碼。

有人能告訴我怎麼解決這個問題嗎? :)

問候

+0

看看:http://stackoverflow.com/questions/5078786/showing-instead-of/5078800#5078800 – 2011-03-23 15:05:44

回答

5

你必須告訴你的EntityManager明確要使用UTF-8。你不顯示任何代碼,所以我只是給你我的片段:

// $entityManager is an instance of EntityManager 
// Add UTF8 handler to EntityManager 
$entityManager->getEventManager()->addEventSubscriber(
       new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit('utf8', 'utf8_unicode_ci') 
); 

這對我有效。另外,確保你只使用PHP的多字節函數(http://php.net/manual/en/book.mbstring.php)。

+0

由於使用了MysqlSessionInit,我將該解決方案適用於MySQL數據庫,不是嗎? – 2011-12-21 21:50:25

+0

@PetrPeller,是的,這完全是與MySQL相關的。 – Ocramius 2012-01-31 08:19:23

+0

太棒了!保存我的一天,非常感謝@Rene。 – ImLeo 2016-09-08 04:46:37