在一个全新的项目(Symfony 4.1)中,我尝试使用 SQLite 数据库。 因此,我将 .env 更改为:
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
#DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db
###< doctrine/doctrine-bundle ###
文件 doctrine.yaml 外观
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_sqlite'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
但是当我尝试创建数据库时:
php bin/console doctrine:database:create
发生此错误:
In CreateDatabaseDoctrineCommand.php line 78:
Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.
事实是url: '%env(resolve:DATABASE_URL)%'
无法递归解析我的 .env 配置的%kernel.project_dir%
部分。
这似乎是一个已知问题,已在2017年为Symfony 3.4修复(因此,我敢打赌,自2017年以来,该问题已在4.1等最新分支中得到解决)
有趣的来源:
https://github.com/symfony/flex/issues/129
https://github.com/symfony/symfony/pull/23901
请注意,我在文件 doctrine.yaml 中找到了两种解决方法:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): 'sqlite:///%kernel.project_dir%/var/data.db'
但是明确(如前所述),我不应该修改该参数... 或者
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_sqlite'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: 'sqlite:///%kernel.project_dir%/var/data.db'
当然,在 .env 文件中填充sqlite connexion字符串比 doctrine.yaml 文件更干净,因为这取决于环境设置... < / p>
您是否知道如何使其在 .env 文件中起作用?