【问题标题】:Configure DBAL via Symfony2 to set charset通过 Symfony2 配置 DBAL 以设置字符集
【发布时间】:2011-06-09 02:09:16
【问题描述】:

有人知道在 Symfony2(symfony 重新加载)yml 配置文件中配置 DBAL/Doctrine2 以执行“设置名称”查询的方法吗?这个问题已经在其他地方问过,但我找不到正确的答案。

http://fossplanet.com/f6/%5Bsymfony-users%5D-symfony2-sandbox-database-collation-49626/

如果没有这样的配置选项,我该如何使用 PHP 来实现呢?或者更好:在 Symfony2 项目中,哪里是执行此操作的正确位置?

【问题讨论】:

    标签: php mysql utf-8 doctrine-orm symfony


    【解决方案1】:

    这还不可能。我正在努力允许这样做,很快就会有可能。

    【讨论】:

    • 感谢您的回复!在我上面链接的示例中,作者正在其他地方进行这种配置,您能想象一下什么地方适合做这种配置吗?我认为必须有一些用 symfony2/doctrine2/mysql 做 utf8 的方法!?
    • 好的,前几天给 Symfony 打了一个补丁,语法是:doctrine.dbal:connections:default:charset:UTF-8
    • 我没有使用 Symfony,那么我必须在哪里设置编码?目前我在 PDO 的属性中使用\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",但它看起来不是最好的解决方案。
    【解决方案2】:

    好的,仅针对可能遇到此问题的其他任何人。这就是我所做的:

    我最终继承了Symfony\Bundle\FrameworkBundle\Controller\Controller 并引入了方法getEntityManager

    public function getEntityManager()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        static $utf8_set = false;
        if (!$utf8_set) {
            $em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8','utf8_unicode_ci'));
            $utf8_set = true;
        }
        return $em;
    }
    

    所以每次我想访问 EntityManager 或我的控制器中的存储库(当然现在是 DoctrineController 的子类)时,我都会调用

    $this->getEntityManager()
    

    分别。

    $this->getEntityManager()->getRepository('What\Ever\Entity\I\Am\Looking\For')
    

    【讨论】:

    • 这在DoctrineControllerTrait 中会更好。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 2015-06-13
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多