简体   繁体   English

通过Symfony2配置DBAL以设置字符集

[英]Configure DBAL via Symfony2 to set charset

Does anybody know a way of configuring DBAL/Doctrine2 in a Symfony2 (symfony-reloaded) yml config file to execute a "set names" query? 有没有人知道在Symfony2(symfony-reloaded)yml配置文件中配置DBAL / Doctrine2以执行“set names”查询的方法? This question has been asked in other places, but I could not find a correct answer. 在其他地方已经问过这个问题,但我找不到正确的答案。

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

If there is no such config option, how can I implement this using PHP? 如果没有这样的配置选项,我该如何使用PHP实现它? Or better: Where is the right place in a Symfony2 project to do this? 或者更好:Symfony2项目中正确的位置在哪里才能做到这一点?

That is not possible yet. 那是不可能的。 I am working on allowing this already, will be possible soonish. 我正在努力允许这个,很快就会有可能。

Ok, just for anybody else who might run into this problem. 好的,只是为了可能遇到这个问题的其他人。 This is what I did: 这就是我做的:

I ended up subclassing Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller and introduced the method getEntityManager : 我最后Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerSymfony\\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;
}

So everytime I am want to access the EntityManager or a repository in my controllers (which of course now subclass DoctrineController ) I call 所以每次我想访问EntityManager或我的控制器中的存储库(当然现在是子类DoctrineController )我打电话

$this->getEntityManager()

resp. RESP。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM