与parameters.yml和config_(dev | stage | prod).yml的数据库连接混淆 [英] Confusion with parameters.yml and config_(dev|stage|prod).yml for database connections

查看:2397
本文介绍了与parameters.yml和config_(dev | stage | prod).yml的数据库连接混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道为什么我应该保持我的数据库连接在 parameters.yml 文件,但我还想要设置额外的数据库连接以及不同的主机dev,stage和prod环境。我想我可以用 config_dev.yml config_test.yml config_prod。 yml 结合 config.yml ,我错了吗?



parameters.yml 我拥有主数据库连接,但我可以移动到配置?



示例: config.yml

 #Doctrine配置 - 注意这里没有定义主机
doctrine:
dbal:
default_connection:blah
连接:
blah:
驱动程序:pdo_pgsql
port:5432
dbname:blah
用户:blah_user
密码:blah_pass
字符集:UTF8
foo:
driver:pdo_pgsql
port:5432
dbname:foo
user:foo_user
password:foo_pass
charset:UTF8

示例: config_dev.yml

 #Doctrine配置 - 这里我定义dev主机
doctrine:
dbal:
连接:
blah:
host :blah_dev
foo:
host:foo_dev

示例: config_test.yml

 #Doctrine配置 - 这里我定义阶段或QA主机
doctrine:
dbal:
connections:
blah:
host:blah_stage
foo:
host:foo_stage

示例: config_prod.yml


$ b b

 #Doctrine配置 - 这里我定义了prod主机
doctrine:
dbal:
连接:
blah:
host:blah_prod
foo:
host:foo_prod

删除 parameters.yml 中的设置,但是Symfony / Doctrine不喜欢这样。我缺少什么?



现在,如果我在 parameters.yml 中定义了默认的数据库连接可以连接到它,然后

 参数:
database_driver:pdo_pgsql
database_host:blah_prod
database_port:5432
database_name:blah
database_user:blah_user
database_password:blah_pass

和config.yml

  doctrine:
dbal:
driver:%database_driver%
host:%database_host%
dbname:%database_name%
user:%database_user%
password:%database_password%

但现在我放弃了我需要的多个数据库,并且能够从dev切换到stage数据库服务器。 >

我必须缺少一些额外的文档,这是正确的,任何帮助将是巨大的。



与Doctrine的多数据库连接的文档





但是我想避免为 parameters.yml

$ b中的每个dev,stage和prod服务器添加三个数据库选项
$ b

 参数:
database_driver:pdo_pgsql
database_host:blah_dev
database_port:5432
database_name:blah
database_user:blah_user
database_password:blah_pass
#database_driver2:pdo_pgsql
database_host2:blah_stage
#database_port2:5432
#database_name2:blah
#database_user2: blah_user
#database_password2:blah_pass
#database_driver3:pdo_pgsql
database_host3:blah_prod
#database_port3:5432
#database_name3:blah
#database_user3:blah_user
#database_password3:blah_pass

(也许只是配置不同的主机? )



这看起来很丑陋,一个PITA,这只是为blah数据库,我必须这样做foo和任何其他数据库我需要配置。 p>

解决方案

如果你的连接在每个环境中是相同的,但只有配置值不同,你应该使用parameters-configuration-file。定义环境变量,你需要在parameters.yml.dist文件中这样:

 #parameters.yml.dist 
参数:
database_host_1:blah
database_host_2:blub
...

您的config.ml可能如下所示:

 #config.yml 
import:
- {resource:parameters.yml}
...
doctrine:
dbal:
default_connection:blah
连接:
blah:
driver:pdo_pgsql
port:5432
host:%database_host_1%
dbname:blah
用户:blah_user
密码:blah_pass
charset: UTF8
foo:
driver:pdo_pgsql
port:5432
host:%database_host_2%
dbname:foo
user:foo_user
密码:foo_pass
charset:UTF8

如果您的parameters.yml缺失,



如果您想将所有配置保存在单独的文件中,请在下次调用 composer update 时请求数据库主机名。您应该将它添加到config_XXX.yml文件的开头:

 #config_XXX.yml 
import:
- {resource:config.yml}
- {resource:parameters_XXX.yml}

将XXX替换为您的环境。为每个环境创建一个parameters_XXX.yml,并为其中的主机设置配置参数,就像默认的parameters.yml文件中一样。

 #parameters_XXX.yml 
parameters:
database_host_1:blahInXXXEnv
database_host_2:blubInXXXEnv
...


I understand why I should keep my database connections in the parameters.yml file but I also wanted to setup additional database connections as well as different hosts for dev, stage and prod environments. I thought I could do this with the config_dev.yml, config_test.yml and config_prod.yml in conjunction with config.yml, am I wrong?

In parameters.yml I hold the main database connection but can I move this to the config(s) instead?

Example: config.yml

# Doctrine Configuration - notice there is no host defined here
doctrine:
    dbal:
      default_connection: blah
      connections:
        blah:
          driver:   pdo_pgsql
          port:     5432
          dbname:   blah
          user:     blah_user
          password: blah_pass
          charset:  UTF8
        foo:
          driver:   pdo_pgsql
          port:     5432
          dbname:   foo
          user:     foo_user
          password: foo_pass
          charset:  UTF8

Example: config_dev.yml

# Doctrine Configuration - Here I define the dev hosts
doctrine:
    dbal:
      connections:
        blah:
          host: blah_dev
        foo: 
          host: foo_dev

Example: config_test.yml

# Doctrine Configuration - Here I define the stage or QA hosts
doctrine:
    dbal:
      connections:
        blah:
          host: blah_stage
        foo: 
          host: foo_stage

Example: config_prod.yml

# Doctrine Configuration - Here I define the prod hosts
doctrine:
    dbal:
      connections:
        blah:
          host: blah_prod
        foo: 
          host: foo_prod

Now I have also removed the settings in parameters.yml but Symfony / Doctrine doesn't like this. Am I missing something? How can I setup something like I have?

Now if I define the default database connection in parameters.yml I can connect to it and then

parameters:
    database_driver: pdo_pgsql
    database_host: blah_prod
    database_port: 5432
    database_name: blah
    database_user: blah_user
    database_password: blah_pass

and in config.yml

doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"

But now I loose the multiple databases I need and the ability to switch from dev to stage to prod database servers.

I must be missing some additional documentation where this is being addressed, any help would be great.

I've seen the documentation for Multiple Database connections with Doctrine

But I would like to avoid having to add three database options like below for each dev, stage and prod server(s) in parameters.yml

parameters:
    database_driver: pdo_pgsql
    database_host: blah_dev
    database_port: 5432
    database_name: blah
    database_user: blah_user
    database_password: blah_pass
    #database_driver2: pdo_pgsql
    database_host2: blah_stage
    #database_port2: 5432
    #database_name2: blah
    #database_user2: blah_user
    #database_password2: blah_pass
    #database_driver3: pdo_pgsql
    database_host3: blah_prod
    #database_port3: 5432
    #database_name3: blah
    #database_user3: blah_user
    #database_password3: blah_pass

( Maybe just configure the different hosts? )

This looks ugly and a PITA, and this is just for blah database, I would have to do this for foo and any other databases I need to configure.

解决方案

If your connections are the same in each environment but only the configuration values are different, you should use the parameters-configuration-file. Define the environment variables, you need, in the parameters.yml.dist file like this:

# parameters.yml.dist
parameters:
    database_host_1: blah
    database_host_2: blub
    ...

Your config.ml may look like this:

# config.yml
imports:
    - { resource: parameters.yml }
...
doctrine:
    dbal:
  default_connection: blah
  connections:
    blah:
      driver:   pdo_pgsql
      port:     5432
      host:     "%database_host_1%"
      dbname:   blah
      user:     blah_user
      password: blah_pass
      charset:  UTF8
    foo:
      driver:   pdo_pgsql
      port:     5432
      host:     "%database_host_2%"
      dbname:   foo
      user:     foo_user
      password: foo_pass
      charset:  UTF8

If your parameters.yml is missing, you'll be ask for the database hostnames next time you call composer update for example.

If you would like to save all your configurations in separate files, you should add this at the beginning of your config_XXX.yml files:

# config_XXX.yml
imports:
    - { resource: config.yml }
    - { resource: parameters_XXX.yml }

Replace XXX with your environment. Create a parameters_XXX.yml for each environment and set the configuration parameters for your hosts in there like it is in the default parameters.yml file.

# parameters_XXX.yml
parameters:
    database_host_1: blahInXXXEnv
    database_host_2: blubInXXXEnv
    ...

这篇关于与parameters.yml和config_(dev | stage | prod).yml的数据库连接混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆