UTF-8 Zend 框架和原则 2
使用 Doctrine 从数据库查询 UTF-8 字符时,我无法正确显示它们。我已经尝试了在互联网上可以找到的所有内容(例如:setCharset似乎是最好的选择)。如果我直接从 PHP 打印 UTF-8 字符,它们会正确显示,因此这不是输出到浏览器的问题。以下是我的配置:
application.ini
doctrine.conn.host = 'localhost'
doctrine.conn.user = 'someuser'
doctrine.conn.pass = 'somepass'
doctrine.conn.driver = 'pdo_mysql'
doctrine.conn.dbname = 'zoo'
doctrine.path.models = APPLICATION_PATH "/models"
学说的引导方法
$classLoader = new \Doctrine\Common\ClassLoader(
'Doctrine',
APPLICATION_PATH . '/../library/'
);
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl( $cache );
$config->setQueryCacheImpl( $cache );
$driver = $config->newDefaultAnnotationDriver(
APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl( $driver );
$config->setProxyDir( APPLICATION_PATH . '/models/Proxies' );
$config->setAutoGenerateProxyClasses( true );
$config->setProxyNamespace( 'App\Proxies' );
$connectionSettings = $this->getOption( 'doctrine' );
$conn = array(
'driver' => $connectionSettings['conn']['driver'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['host'],
);
$entityManager = \Doctrine\ORM\EntityManager::create( $conn, $config );
$entityManager->getConnection()->setCharset('utf8');
$registry = Zend_Registry::getInstance();
$registry->entityManager = $entityManager;
return $entityManager;
非常感谢任何帮助。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
doctrine.conn.driverOptions.1002 = "SET NAMES 'UTF8'"
怎么样?也许有帮助。哦,我看到,你正在手动引导它。您还必须在连接数组中添加线路。
也许您会看看 Bisna-library (教程如何使用它),它可以帮助您将doctrine2集成到采埃孚。
What about
doctrine.conn.driverOptions.1002 = "SET NAMES 'UTF8'"
? Maybe it helps.Oh i saw, that you're bootstrapping it manually. You have to add the lines in the connection array too.
Maybe you will have a look into the Bisna-library (Tutorial how to use it), it helps you to integrate doctrine2 into the ZF.
大约 6 个月前,我遇到了同样的问题,我通过在
application.ini
resources.db.params.charset = "utf8"
中编写以下行解决了这个问题可能会帮助你......
I was facing the same problem about 6 months ago, and I solved this problem by writing following line at
application.ini
resources.db.params.charset = "utf8"
May help you...
我也为此苦苦挣扎了很长时间,但找到了适合我的情况的解决方案。您必须将 EventSubscriber 添加到您的 EntityManager。
希望这会对您有所帮助。
I was struggling with this for a long time too but found the solutions for my case. You have to add an EventSubscriber to your EntityManager.
Hopefully this will help you.