Julian
Posted on December 6, 2019
most of the time you need to set the following variables:
doctrine:
dbal:
dbname: database
host: localhost
port: 1234
user: user
password: secret
driver: pdo_mysql
server_version: '5.6'
charset: UTF8
url: mysql://db_user:db_password@127.0.0.1:3306/db_name
https://symfony.com/doc/current/reference/configuration/doctrine.html
there are quite a lot settings to configure doctrine in symfony. if you want to be flexible, your .env file or your
environment variables get increased by many variables.
the main problem is that if you want to use sqlite for tests and mysql for dev & prod you need to define the charset
and
the server_version
. where sqlite needs UTF8
and mysql UTF8MB4
. also mysql needs the server_version
an important info is, that url overwrites all other settings. so if your url starts with mysql:
you don't need to set
the driver
A URL with connection information; any parameter value parsed from this string will override explicitly set parameters
info from Configuration.php
of doctrine-bundle
a thing which is well hidden, is that doctrine supports query parameters, so you can reduce the config to this:
.env
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.sqlite?charset=UTF8
#DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name?charset=UTF8MB4&server_version=5.6
with this dotenv setting you also set the charset and the server version
so the other settings from above are reduced to this:
doctrine.yaml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
links:
- cover image by MasterTux https://pixabay.com/photos/adventure-treasure-map-old-world-map-2528477/
Posted on December 6, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 17, 2021