Manage Symfony App Heroku - symfony

I've a Symfony app on Heroku with ClearDb addons. I need to manage the app for test and prod. So I need two database: one for test and one for the production(principle);
I tryed the Heroku Pipeline, but when I promote the app from staging to production, the production app is connetted to staging db. How can solve ?
How you manage it?
EDIT
I discovered the mistake. I set the parameters via
$db = parse_url(getenv('CLEARDB_DATABASE_URL'));
$container->setParameter('database_host', $db['host']);
From a quick search for $container->setParameter I can see that this is a Symfony feature to interpolate values into code, however they mention the following warning in their docs:
NOTE: You can only set a parameter before the container is compiled:
not at run-time. To learn more about compiling the container see
Compiling the Container.
https://symfony.com/doc/current/service_container/parameters.html#getting-and-setting-container-parameters-in-php
Heroku handle only symfony apps in prod env. So the stage app also have the environment var as "prod". How can I set parameters for different env? Or dynamically?
Thanks,
AlterB

I've solved with the Environment Variables.
I changed into app/config/config.yml this:
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
with that
doctrine:
dbal:
driver: pdo_mysql
url: '%env(CLEARDB_DATABASE_URL)%'
The app gets the db connection directly from Heroku Env. And it's done!

Related

DB create Doctrine2

I'm new to the Symfony and Doctrine world.
So i'm following the doc to install Symfony. All went well.
Next Step : create the DB. Again I'm following the documentation (Databases and the Doctrine ORM) and run the command php bin/console doctrine:database:create
This command give me no output and is running for more than 30 minuts which seems to be a lot just to create a DB.
I've got a local mysql server running on 127.0.0.1:3000
here is my .env file
###> doctrine/doctrine-bundle ###
# Format described at https://www.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"
# For a PostgreSQL database, use: "postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:root#127.0.0.1:3000/my_project?serverVersion=8.0.19
###< doctrine/doctrine-bundle ###
Does anyone have a clue why this command does not give me any output ?
Problem solved even if I don't understand why.
I edit the doctrine.yaml that was like
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
to
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: my_project
user: root
password: root
host: localhost
driver: pdo_mysql
server_version: '8.0.19' # your database server version here
#url: '%env(resolve:DATABASE_URL)%'

Symfony4 - opt out of the "environment variables" feature and keep the old parameters.yml file?

In Symfony4 they chose to replace the parameters.yml with environment variables, as said in the docs and in the migration guides:
Define the infrastructure-related configuration options as environment
variables. During development, use the .env file at the root of your
project to set these.
I really don't like this change, I don't see any advantage but only drawbacks (for example we'd need to refactor the app because now only scalar values are allowed as environment variables, you can't host different installations in the same environment, etc...)
So my question is, is it possible to keep the old good parameters.yml file in Symfony4? How can I include this file and refer to his parameters, for example, in the doctrine.yml and swiftmailer.yml?
Done, it's very easy and it makes your migration to Symfony4 much easier.
Just move the parameters.yml and parameters.yml.dist to config/, and rename them to .yaml for consistency.
Then add in services.yaml:
imports:
- { resource: parameters.yaml }
Then everything will remain the same, for example the doctrine.yaml file will be:
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
...
Optional: add https://github.com/Incenteev/ParameterHandler

Load doctrine's entityManager with a specific environment

Inside a test suite, I load doctrine's entity manager via container:
$this->manager = $this->container->get('doctrine.orm.entity_manager');
But this load entity manager for test environment. I want to save new records inside dev environment. I suppose the solution is to load entityManager with dev environment. But how?
You can specify your doctrine configuration for Test environment inside config_test.yml file:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: '%database_test_host%'
port: '%database_test_port%'
dbname: '%database_test_name%'
user: '%database_test_user%'
password: '%database_test_password%'
charset: UTF8

SonataMediaBundle You have requested a non-existent parameter "doctrine.connections" error

I have setup a SonataAdminBundle on my Symfony 2.1.2 project and it works correctly. Now I´m trying to setup a SonataMediaBundle but I get this error:
==> php app/console sonata:easy-extends:generate SonataMediaBundle
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "doctrine.connections".
I copied the config parameters to config.yml as indicates the documentation. You can see it there: http://pastebin.com/wys11net
Any help or clue?
Thanks in advance
Looks like you're missing the Connections node inside the Doctrine > DBAL's node, aswell the specification of a default connection (among multiple connections, if that would be the case).
An example of the right template would be like
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
I just ran into this problem as well, and found that the "connections" node is not actually required in the config (see http://symfony.com/doc/current/reference/configuration/doctrine.html#doctrine-dbal-configuration ... you only need the connections node when you have multiple connections).
In my case, it was that I had the SonataCacheBundle in the AppKernel.php file before Doctrine. Doctrine sets the "doctrine.connections" parameter when Doctrine initializes, so if you try to access it in SonataCacheBundle before Doctrine has initialized then "doctrine.connections" is not in the container yet.
Reordering the entries in AppKernel.php fixed the issue for me.

How to use sqlite database on symfony2 project?

In a Symfony2 project, you can configure the databases connections at the app/config/parameters.ini file. Documentation states that you can use, among others, sqlite3 PDO driver.
But configuring sqlite doesn't works well:
[parameters]
database_driver = pdo_sqlite
database_host = localhost
database_port =
database_name = test_project.db
database_user = root
database_password =
Using app/console doctrine:database:create, successfully creates a test_project.db file at the project root directory.
But after creating some entities, then running app/console doctrine:schema:update --force should create the tables on the database file, but it doesn't, file appears empty, with O bytes size.
Note that using any other PDO driver works well, but not with SQLite...
I've also tried to use the full path for the db file in the database_name parameter, but to no avail, database still doesn't gets updated.
For reference, here's the doctrine dbal section of the config.yml file:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
Is there a way around this? configurations missing? something not stated on the official doc of symfony2 project?
According to Doctrine the elements used for sqlite DBAL configuration are:
user (string): Username to use when connecting to the database.
password (string): Password to use when connecting to the database.
path (string): The filesystem path to the database file. Mutually exclusive with memory. path takes precedence.
memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.
This is also listed in the full reference for Doctrine configuration in Symfony2, although not elaborated on.
So you need to switch up your config params to match whats appropriate for sqlite.
Here is what I needed to get SQLite to work, just after doing symfony new myapp :
in app/config.yml :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_sqlite
path: "%database_path%"
In app/config/parameters.yml:
parameters:
database_path: "%kernel.root_dir%/db/myapp_%kernel.environment%.db3"
...
Next I could do a composer install, create a new entity and it just worked.
I've found that if I add a path line pointing at the database_name to my config.yml, sqlite seems to pick that up, and MySQL doesn't seem to complain.
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
path: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
This means you can still keep all database information in the parameters file, you don't need separate configs depending on which database you are using.
Mainly the file path or the file path permisssion will have issue.
In config.yml, set path to full path like
/home/{name}/NB/PHP/Symfony/test/src/Database/data.db3
Dont give %database_path% or what ever. Try this it will work.
If it works you can give as
%kernel.root_dir%/../src/Database/%database_path%
Also check sqlite is ok by
phpinfo(INFO_MODULES);
In view/output you can see pdo_sqlite and its version.
In my case setting a username and password in config/packages/doctrine.yaml did not create a username/password protected sqlite database.
doctrine:
dbal:
charset: UTF8
url: '%DATABASE_URL%'
user: 'foo'
password: 'bar'
It seems like the parameters username and password are ignored?

Resources