开发者

Configure DBAL via Symfony2 to set charset

开发者 https://www.devze.com 2023-02-04 13:26 出处:网络
Does anybody k开发者_运维问答now a way of configuring DBAL/Doctrine2 in a Symfony2 (symfony-reloaded) yml config file to execute a \"set names\" query? This question has been asked in other places, bu

Does anybody k开发者_运维问答now a way of configuring DBAL/Doctrine2 in a Symfony2 (symfony-reloaded) yml config file to execute a "set names" query? 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/

If there is no such config option, how can I implement this using PHP? Or better: Where is the right place in a Symfony2 project to do this?


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:

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

$this->getEntityManager()

resp.

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

精彩评论

暂无评论...
验证码 换一张
取 消