petekaner
petekaner

Reputation: 8921

Symfony 4: how to use two different environments in my local server

I recently started working with Symfony 4.

While developing I like to have two different environments: one for dev (surfing the site with some fake data that we have on fixtures) and one for testing where I execute behat tests that reset the database on each test.

In previous versions of symfony I just had two different databases: mysite_dev and mysite_test and two parameters files (parameters_dev.yml and parameters_test.yml) with the proper database configuration.

But with Symfony 4 and the .env configuration file I don't see the way of doing this. I don't see any way of having two sets of ddbb params and be able to run php bin/console doctrine:schema:update --env=test or php bin/console doctrine:schema:update --env=dev.

Any ideas? thanks!

Upvotes: 2

Views: 2512

Answers (2)

Brucie Alpha
Brucie Alpha

Reputation: 1236

how about using different url variables in your config?

# config/packages/dev/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL_DEV)%'

# config/packages/test/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL_TEST)%'

also consider this comment from the default config file

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): ''

Upvotes: 2

DonCallisto
DonCallisto

Reputation: 29932

It's quite easy: your commands can still reference an environment with --env and everytime you need to switch from one environment to other you have basically two choices:

  • Change manually the .env file
  • Create a second "vhost" (or equivalent) where you can store all variables you would normally place into .env as described here

Upvotes: 0

Related Questions