DEV Community

Cover image for Full Doctrine configuration via url

Posted on

Full Doctrine configuration via url

most of the time you need to set the following variables:

        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@

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:



with this dotenv setting you also set the charset and the server version

so the other settings from above are reduced to this:


        url: '%env(resolve:DATABASE_URL)%'


Top comments (0)