gpt4 book ai didi

php - UTF-8 Zend 框架和 Doctrine 2

转载 作者:行者123 更新时间:2023-11-29 04:12:04 25 4
gpt4 key购买 nike

当使用 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;

非常感谢任何帮助。

谢谢。

最佳答案

doctrine.conn.driverOptions.1002 = "SET NAMES 'UTF8'" 怎么样?也许有帮助。

哦,我看到了,您正在手动引导它。您还必须在连接数组中添加这些行。

也许你会看看 Bisna-library ( Tutorial how to use it ),它可以帮助您将 doctrine2 集成到 ZF 中。

关于php - UTF-8 Zend 框架和 Doctrine 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com