UTF-8 Zend 框架和原则 2

发布于 2024-12-10 02:31:34 字数 1846 浏览 0 评论 0 原文

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

非常感谢任何帮助。

谢谢。

I am failing to get UTF-8 characters to display correctly when querying them from the database using Doctrine. I have tried everything I could find on the internet (Eg: setCharset seemed to be the best option). If I print UTF-8 characters directly from PHP they are displayed correctly, so it is not a problem of output to the browser. Here are my configurations:

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"

bootstrap method for doctrine

    $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;

Any help is highly appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心小果奶 2024-12-17 02:31:34

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.

巷子口的你 2024-12-17 02:31:34

大约 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...

长梦不多时 2024-12-17 02:31:34

我也为此苦苦挣扎了很长时间,但找到了适合我的情况的解决方案。您必须将 EventSubscriber 添加到您的 EntityManager。

$entityManager = \Doctrine\ORM\EntityManager::create( $conn, $config );

$entityManager->getEventManager()
              ->addEventSubscriber(
                  new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit('utf8', 'utf8_unicode_ci')
                );

希望这会对您有所帮助。

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.

$entityManager = \Doctrine\ORM\EntityManager::create( $conn, $config );

$entityManager->getEventManager()
              ->addEventSubscriber(
                  new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit('utf8', 'utf8_unicode_ci')
                );

Hopefully this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文