Doctrine insists on trying to use PostgreSQLDriver despite contrary config - php

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.

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

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.

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

Related

Symfony 5, problem with doctrine for migration to database mariadb

I have a problem with symfony 5.
When I execute the command: php bin/console doctrine:migrations:migrate
I have an error:
In MetadataStorageError.php line 13: The metadata storage is not up to
date, please run the sync-metadata-storage command to fix this issue.
and I modify my env.local to specify the server version with MariaDB:
DATABASE_URL=mysql://db_user:.......#127.0.0.1:3306/bundletest?serverVersion=mariadb-10.5
and when I execute: php bin/console doctrine:migrations:migrate
I have another error:
In DBALException.php line 86:
Invalid platform version "mariadb-10.5" specified. The platform version has to be specified in the format:
"^(?:5.5.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>".
Setting up doctrine migrations
same way to configure postgres and mysql (need to set different PDO driver of course, server version)|
here is tested & working example up to symfony 5.2 & PHP 7.4
you need two things to update in your project
after instaling doctrine you got new config file
project_dir/config/packages/doctrine.yaml
please set is as below
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
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
and in your .env file
#DATABASE_URL="mysql://db_user:db_password#127.0.0.1:3306/db_name"
uncoment, set your user and db settings, add ?serverVersion
DATABASE_URL="mysql://db_user:db_password#127.0.0.1:3306/db_name?serverVersion=5.7
run
php bin/console doctrine:database:create (it is better to create db with your project config settings)
and now you are free tu use migrations

php bin/console doctrine:database:create Operation timed out or Connection refused [duplicate]

This question already has answers here:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
(29 answers)
Closed 2 years ago.
I am trying to create a table in my mysql using doctrine.
(I am learning symfony using this video in French: https://www.youtube.com/watch?v=UTusmVpwJXo , it seems that the video is based on a previous version of synfony but hey, should be almost the same right).
So the command I am running is: php bin/console doctrine:database:create
Unfortunately I have these results:
doctrine.yaml
doctrine:
dbal:
default_connection: default
driver: 'pdo_mysql'
url: '%env(resolve:DATABASE_URL)%'
server_version: '7.4.4'
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
and then my .env
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=b3b1c82ba50949ebe4f89ac492c6c7f6
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
#TRUSTED_HOSTS='^(localhost|example\.com)$'
###< symfony/framework-bundle ###
###> symfony/mailer ###
# MAILER_DSN=smtp://localhost
###< symfony/mailer ###
###> 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:#127.0.0.1:3306/blog"
###< doctrine/doctrine-bundle ###
I didn't set any password, and my phpmyadmin is running correctly at http://127.0.0.1:8080/phpmyadmin/
And my mysql conf looks like this:
Maybe it changes anything (but I don't think so), my main page is running on http://127.0.0.1:8000/blog and runs good. I started the server with symfony server:start -d.
Do you guys know what I could have done wrong here?
Thank you for any future help!!
Stay strong in this hard times and learn more!
I finally found my solution here -> Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
It was a default behavior of sql as a security process.
I follow the exact step described in the answer and it worked.

Symfony 4 Doctrine, creating Entities from database table

New to Symfony here
I am following this tutorial about mapping db tables to entities. I created the blog_post and blog_comment tables in my db.
I tried running this php bin/console doctrine:mapping:import --force AppBundle xml which spit out the following error.
Bundle "AppBundle" does not exist or it is not enabled. Maybe you forgot to
add it in the registerBundles() method of your App\Kernel.php file?
My doctrine.yaml is pretty much out of the box.
Doctrine.yaml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# With Symfony 3.3, remove the `resolve:` prefix
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: true
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
I created the App.php Class under src/Entity/ and added a line under bundles.php but still get an error. The line I added on bundles.php is
App\Entity\App::class=>['all' => true], since that is the defined namespace of my App.php class.
Another error I get is
PHP Fatal error: Uncaught
Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load
class "App" from namespace "App\Entity\App".
Did you forget a "use" statement for another namespace? in
/var/www/html/quick_tour/src/Kernel.php:32
you must use App rather than AppBundle on Symfony 4
Found similar situation to mine in here as well.
Since Symfony 4 Use App instead of Appbundle

Symfony4 - Error: "Notice: Undefined index: dbname" in AbstractOracleDriver.php while trying to create database

I am using Symfony 4.0.3 and I need connect to Oracle DB. I follow this instructions:
https://symfony.com/doc/current/doctrine.html
my .env file
DATABASE_URL=oci8://user:pwd#xxx.xxx.xxx.xxx:1521/something
host address is from tnsnames.ora -> HOST, something is from tnsnames.ora -> SERVICE_NAME
And after running command:
php bin/console doctrine:database:create
I get this error:
C:\Apache24\htdocs\myapp>
2018-01-25T12:58:47+00:00 [error] Error thrown while running command
"doctrine:d
atabase:create". Message: "Notice: Undefined index: dbname"
In AbstractOracleDriver.php line 125:
Notice: Undefined index: dbname
I am able to connect to DB by plain PHP, but through Symfony not. dbname is something from my connect url at .env file. This is set from tnsnames.ora.
It looks like the dbname parameter is not picked up from your environment variable / DBAL url.
To solve this quickly just use explicit configuration instead of URL for doctrine DBAL's configuration.
The full reference configuration can be found in the symfony documentation -
Doctrine DBAL Reference Configuration
The documentation for the Doctrine DBAL Oracle configuration can be found here.
config/packages/doctrine.yaml:
doctrine:
# [..]
dbal:
default_connection: oracle_db_1
connections:
oracle_db_1:
driver: 'oci8' # ... or 'pdo_oci'
dbname: '<dbname>'
host: '<host>'
port: '<port>'
user: '<user>'
password: '<password>'
If that works replace dbname, host, port etc. with container parameters (i.e. '%env(DATABASE_NAME)%') read from environment variables and add those to your .env file.
doctrine.yaml edited like this:
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): ''
doctrine:
dbal:
default_connection: default
connections:
default:
driver: 'oci8' # ... or 'pdo_oci'
dbname: 'dbname'
host: 'xxx.xxx.xxx.xxx'
port: '1521'
user: 'user'
password: 'pwd'
# With Symfony 3.3, remove the `resolve:` prefix
# url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
after command
php bin/console doctrine:database:create
I have same error.
2018-01-26T06:23:31+00:00 [error] Error thrown while running command "doctrine:d
atabase:create". Message: "Notice: Undefined index: dbname"
In AbstractOracleDriver.php line 125:
Notice: Undefined index: dbname
my dbname is same as I have in SQL Developer in SID item.
There is a bug in doctrine/dbal version <= 2.8.0.
Upgrade to doctrine/dbal to version 2.9.0 and it should start to work.
Here is the github pull request solving this issue:
https://github.com/doctrine/dbal/pull/3297
If you use the SERVICE_NAME for your connection, you can define the DATABASE_URL like this:
DATABASE_URL=oci8://user:pw#x.x.x.x:1521/?service=1&servicename=something&charset=AL32UTF8

Symfony 4 Doctrine not working from console [2002] No such file or directory

I´m working with symfony 4 and this error ocurr when runnning doctrine console commands:
In AbstractMySQLDriver.php line 108:
An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory
I think this is because there´s something wrong with the connection but when I run the application it has access to the database without errors, so I can not imagine what is wrong with the connection.
I was able to continue working by creating manually the database and using
php bin/console doctrine:schema:create --dump-sql
But this is really unwanted because later when the schema needs to be updated all data would be lost because i would need to re-create the schema instead of updating it.
The app works normally after executing manually the SQL (easyadmin works fine).
Here are my configurations:
.ENV:
DATABASE_URL=mysql://root:root#127.0.0.1:3306/test
doctrine.yaml:
parameters:
env(DATABASE_URL): ''
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
I tried the drop command just to test and it throwed this:
In DropDatabaseDoctrineCommand.php line 93:
Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.
So I add dbname to doctrine.yaml and didn´t worked.
I also tried removing the cache, creating a new project, using a remote mysql, and using sqlite but the errors are the same in both cases !!
Thanks in advance for the help !
Please change DB_HOST=localhost to DB_HOST=127.0.0.1
> doctrine/doctrine-bundle
DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}#${DB_HOST}:${DB_PORT}/${DB_NAME}
So i suppose that you showed the whole content of your doctrine.yml.
If so, you might want to consider changing your doctrine.yml as follows:
add an additional line in the dbal: section
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(resolve:DATABASE_URL)%'
Watch the last line, beginning with url:, closely.
The 'resolve:" operator looks up DATABASE_URL in your env-Variables and if it is found, it then replaces the expression with its content.
This is a doctrine.yml, that gets auto-generated by symfony.
The 'resolve' operator migth not be needed though. This could potentially work too:
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(DATABASE_URL)%'
This method is proposed in the symfony docs, which might be interesting to read, to understand further configuration of symfony.
My fix is here:
export DATABASE_URL=<value from .env>
And generating SQLs works :)
config/packages/doctrine.yaml
I solved this error adding my database url in the "config/packages/doctrine.yaml" file the "parameters" > "env(DATABASE_URL)" field:
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): 'mysql://your_db_url'
I solved this change in .env file
DATABASE_URL=mysql://root:root#127.0.0.1:3306/test
to
DATABASE_URL="mysql://root:root#127.0.0.1:3306/test"
I added quotes. Maybe you are using windows? It works for me.
like in Symfony docs: Environment Variables
Hi On my side I add the exact same behaviour, on symfony 4,
I just changed this
DATABASE_URL=mysql://root:root#localhost:8889/
to this
DATABASE_URL=mysql://root:root#127.0.0.1:8889
on my .env or .env.dev file (depend on the env you are working on)
hope it helps.

Categories