0

I am setting up a Symfony 5.2 app, using doctrine/doctrine-bundle 2.4 and doctrine/orm 2.9. My doctrine.yaml config file looks like this:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        driver: 'pdo_mysql'
        server_version: '5.7'
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

The problem is that when I run doctrine:fixtures:load, I get the following output:

In AbstractPostgreSQLDriver.php line 102:

  An exception occurred in driver: could not find driver


In Exception.php line 18:

  could not find driver


In PDOConnection.php line 39:

  could not find driver

It looks like Doctrine is trying to use the Postgre driver, though I have specified MySQL. Is there another place where I need to configure this? If so, how?

===

EDIT: It looks like DATABASE_URL is not available inside my Docker container, which is probably the source of the problem. I've verified that DATABASE_URL is defined in my .env file, and that my .env file is in the same folder as my docker-compose.yml file. Also, I have a setting like this for the environment variable in that docker-compose file:

environment:
  - DATABASE_URL

So I'm not quite sure what's wrong here.

  • So you accepted an answer which is great but you should probably indicate the actual docker solution someplace. – Cerad Jul 11 '21 at 00:17
  • To be honest, I don't remember exactly what fixed it, since I tried a bunch of stuff. It was some combination of: Making sure values in .env files weren't quoted; making sure the .env file was in the correct place; making sure all environmental variables were passed through by the "environment" attribute in docker-compose.yml. Thanks for all the help! – Mayor of the Plattenbaus Jul 11 '21 at 08:52

3 Answers3

1

I think you have a wrong DATABASE_URL in your .env file. Please check if it starts with mysql://.

jDoe
  • 113
  • 5
  • Thanks for your answer! I just checked, and the .env file contains this `DATABASE_URL=mysql://db-user:db-password@database/db-name`. So I don't think this is the problem. – Mayor of the Plattenbaus Jul 10 '21 at 12:37
  • @Patrick The postgress stuff is coming from the default DATABASE_URL. Typically you would create a .env.local to set database credentials. Might check to see if you have that file. Maybe try clearing the cache as well. I'm kind of guessing that you really did not name your database 'db-name'? – Cerad Jul 10 '21 at 13:00
  • I actually am naming my database db-name, since this is just going to be a development environment. But I think I've got a bigger problem. When I go into my docker container and run `echo $DATABASE_URL` I just get a blank line, despite the .env file having a valid value in it. – Mayor of the Plattenbaus Jul 10 '21 at 13:21
  • Ah yes. Docker often adds to the fun. There are many places where ENV variables can be specified. Time to do some grepping for APP_ENV. – Cerad Jul 10 '21 at 15:19
1

I believe symfony uses dotenv package to load the values from .env to the S_ENV superglobal. You need to debug whether your dotenv (and there is a .env in your project and it is not .env.local or .env.dist) is loaded properly.

Hisham
  • 411
  • 3
  • 9
1

It has happened to me sometimes when I was starting, typically when cloning a Github project which has a default .env file created by Doctrine when it's installed through composer that by default Doctrine uses PostgreSQL URL.

See how many .env files have you document and I'm sure that someone overrides your URL connection.

See this StackOverflow post to know the difference between the .env.* files What is the difference between .env.local and .env.development.local

Manuel
  • 135
  • 1
  • 8