How to configure quotes strategy in Doctrine 2

时间:2015-05-12 23:25:59

标签: php mysql postgresql symfony doctrine-orm

I'am try to move from mysql to postgresql and stuck on ansi quotes in upper case fields names.

I read the chapter about Doctrine quotes strategies in the manual. Unfortunately I don't understand where I have to put the configuration code.

In order to get an ansi quote strategy I should use this code:

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
        var id = e.CommandArgument; //Here, I get the id of the previous Row
}

I'm on Symfony 2 — I guess this matters when it comes to configuration.

2 个答案:

答案 0 :(得分:3)

doctrine-bundle 1.5 +。

config.yml

中配置它
doctrine:
    # ...
    orm:
        # ...
        quote_strategy: doctrine.orm.quote_strategy.ansi

对于多个实体经理:

doctrine:
    # ...
    orm:
        # ...
        entity_managers:
            default:
                quote_strategy: doctrine.orm.quote_strategy.ansi

答案 1 :(得分:0)

为了实现这一目标,您将不得不对服务容器进行破坏。 Doctrine包当前不允许您设置引用策略,因此实现此目的的最简洁方法是增加Doctrine Bundle中定义的当前服务。

  1. Create an overriding compiler pass
  2. Enable the compiler pass in your bundle
  3. 我没有时间对这段代码进行全面测试,但是当它完成时它看起来非常像...

    #MyBundle / DependencyInjection / Compiler / DoctrineQuoteStrategyCompilerPass.php

    <?php
    
    namespace MyBundle\DependencyInjection\Compiler;
    
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Definition;
    use Symfony\Component\DependencyInjection\Reference;
    
    class DoctrineQuoteStrategyCompilerPass extends CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            // Create a new service out of the ANSI quote strategy.
            $ansiQuoteStrategy = new Definition(
                'Doctrine\\ORM\\Mapping\\AnsiQuoteStrategy'
            );
            $container->setDefinition('doctrine.ansi_quote_strategy', $ansiQuoteStrategy);
    
            // Add this to orm configuration calls.
            $definition = $container->getDefinition('doctrine.orm.default_configuration');
            $definition->addMethodCall('setQuoteStrategy', array(new Reference('doctrine.ansi_quote_strategy')));
        }
    }
    

    MyBundle / MyBundleBundle.php (*捆绑目录中的Bundle.php文件)

    <?php
    
    use MyBundle\DependencyInjection\Compiler\DoctrineQuoteStrategyCompilerPass;
    
    class MyBundleBundle extends Bundle
    {
        public function build(ContainerBuilder $container)
        {
            ...
    
            $container->addCompilerPass(new DoctrineQuoteStrategyCompilerPass());
        }
    }
    

    警告

    此代码假定您在某处定义了自己的应用程序包(这是最佳实践)。它还假设您只定义了一个实体管理器,这是最有可能的。如果不是这种情况,则必须使用正确的实体管理器名称(将“default”更改为“myname”)。