与parameters.yml和config_(dev | stage | prod).yml的数据库连接混淆 [英] Confusion with parameters.yml and config_(dev|stage|prod).yml for database connections
问题描述
我明白为什么我应该保留我的数据库连接在 parameters.yml
文件,但我还想要设置额外的数据库连接以及不同的主机dev,stage和prod环境。我想我可以用 config_dev.yml
, config_test.yml
和 config_prod。 yml
结合 config.yml
,我错了吗?
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?
在 parameters.yml
我拥有主数据库连接,但我可以移动到配置?
In parameters.yml
I hold the main database connection but can I move this to the config(s) instead?
示例: 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
例如: config_dev.yml
# Doctrine Configuration - Here I define the dev hosts
doctrine:
dbal:
connections:
blah:
host: blah_dev
foo:
host: foo_dev
示例: config_test.yml
# Doctrine Configuration - Here I define the stage or QA hosts
doctrine:
dbal:
connections:
blah:
host: blah_stage
foo:
host: foo_stage
示例: config_prod.yml
# Doctrine Configuration - Here I define the prod hosts
doctrine:
dbal:
connections:
blah:
host: blah_prod
foo:
host: foo_prod
删除 parameters.yml
中的设置,但是Symfony / Doctrine不喜欢这样。我缺少什么?
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?
现在,如果我在 parameters.yml
中定义了默认的数据库连接可以连接到它,然后
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
和config.yml
and in config.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
但现在我松开了我需要的多个数据库,并且能够从dev切换到stage数据库服务器。
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.
与Doctrine的多数据库连接的文档
I've seen the documentation for Multiple Database connections with Doctrine
- http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
但是我想避免为 parameters.yml
>中的每个dev,stage和prod服务器添加三个数据库选项, / p>
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? )
这看起来很丑陋,一个PITA,这只是为blah数据库,我必须这样做foo和任何其他数据库我需要配置。 p>
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.
推荐答案
如果您的连接在每个环境中都是相同的,但只有配置值不同,您应该使用parameters-configuration-file。定义环境变量,你需要在parameters.yml.dist文件中这样:
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
...
您的config.ml可能如下所示:
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
如果您的parameters.yml缺失,
If your parameters.yml is missing, you'll be ask for the database hostnames next time you call composer update for example.
如果您要将所有配置保存在单独的文件中,请在下次调用 composer update 时请求数据库主机名。您应该将它添加到config_XXX.yml文件的开头:
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 }
将XXX替换为您的环境。为每个环境创建一个parameters_XXX.yml,并为其中的主机设置配置参数,就像在默认的parameters.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屋!