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

时间:2011-01-14 11:07:21

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

有没有人知道在Symfony2(symfony-reloaded)yml配置文件中配置DBAL / Doctrine2以执行“set names”查询的方法?其他地方也提出过这个问题,但我找不到正确答案。

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

如果没有这样的配置选项,我该如何使用PHP实现它?或者更好:Symfony2项目中哪个位置正确?

2 个答案:

答案 0 :(得分:4)

这还不可能。我正在努力允许这一点,很快就可能。

答案 1 :(得分:1)

好的,只是对于可能遇到此问题的其他人。这就是我所做的:

我最后继承了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()

RESP。

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