I've declared a configuration in app/config/config_prod.yml so I can execute commands on the remote database.
imports:
- { resource: config.yml }
doctrine:
dbal:
driver: %database_driver%
host: the-ip
dbname: the-database
user: the-user
password: the-password
But when I run php app/console doctrine:schema:update --dump-sql -e=prod it still uses the configuration set in parameters.yml.
A var_dump($this->getApplication() in the execute method in Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand shows the right environment doesn't get the configuration from config_prod.yml
public $parameters =>
array(428) {
...
'kernel.environment' =>
string(4) "prod"
...
'database_host' =>
string(9) "127.0.0.1"
Did I encounter a bug or is this command not capable of dealing with different environments?
Short: You have to clear the cache after every configuration change in the prod environment. In the dev environment the cache is rebuild (some parts not) on every request. But not in prod.
Indeed, configuration is compiled into cache. Loading the configuration from YAML/XML on every request would be too heavy. Look for the method getDefaultParameters() in app/cache/prod/appProdProjectContainer.php and you can see, all configuration parameters are merged in a huge big array.
You will notice, the parameters in this array, are not often the same, you configure in your config.yml. The injecting of these parameters depends on the bundle and their specific extension class. See Semantic Configuration for further infos.
Related
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
I have this configuration for docker:
# docker-compose.yml
environment:
REDIS_SERVER: redis
REDIS_PORT: 6379
And then in symfony I use this to configure parameters:
# parameters.yml
redis_port: %env(REDIS_PORT)%
And then I use the parameter to configure Picmore's cache:
```yaml
# pimcore.yml
cache:
pools:
redis:
enabled: true
connection:
port: '%redis_port%'
But I get the error
Invalid type for path "pimcore.cache.pools.redis.connection.port". Expected int, but got string.
The error is clear but I can't solve it, I tried different options:
redis_port: '%env(REDIS_PORT)%'
redis_port: '%env(int:REDIS_PORT)%'
But I get the same error. How can I fix this?
You are using Symfony 3.3, but the int environment variable preprocessor was not added until Symfony 3.4, according to this.
I don't think you'll be able to use env vars to configure this value, since environment variables are strings by default.
Your options are to either use different parameters files for different environment, as things used to be done back then (which I understand can be inconvenient when using containers); or upgrade to a more recent version of Symfony.
i think this might be a stupid question for some, but i swear that i can't find the answer by myself actually.
I'm still learning Symfony so please be nice.
Actually i have set up the access to my database in the .env file :
[...]
DATABASE_URL=mysql://root:db_password#127.0.0.1:3308/db_name
[...]
And then i have define the environnement variable in the config/doctrine.yaml :
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: Symfony
user: root
password: null
host: localhost
server_version: '8.0'
driver: pdo_mysql
charset: UTF8
So i run the following command :
> php bin/console doctrine:database:create
But the 'Symfony' database have been created under mariadb.
:( I don't understand why because i have define precisely that i use a 8.0 version and this version is not a mariadb version.
I've also try this :
# .symfony/services.yaml
mydatabase:
# supported versions: 5.7, 8.0
type: oracle-mysql:8.0
disk: 1024
As the symfony documentation saying to do, but it still not working at all.
I have got the following error message as mydatabase is not a symfony services :
C:\Users\vincp\Symfony\charming_tuto>php bin/console doctrine:database:create
In FileLoader.php line 173:
There is no extension able to load the configuration for "mydatabase" (in "C:\Users\vincp\Symfony\charming_tuto\con
fig/services.yaml"). Looked for namespace "mydatabase", found ""framework", "sensio_framework_extra", "twig", "twig
_extra", "web_profiler", "monolog", "debug", "webpack_encore", "doctrine", "doctrine_migrations", "maker"" in C:\Us
ers\vincp\Symfony\charming_tuto\config/services.yaml (which is being imported from "C:\Users\vincp\Symfony\charming
_tuto\src\Kernel.php").
I wanna use mysql because this is the DBMS that i use usually.
(If you think that i should use mariadb instead of mysql i'm open to that, but give me some arguments. I'd be really interseted in)
Maybe it is a server issue. So first, make sure that MySQL is running on your server besides MariaDB (how to do it depends on the type of server you are using).
If yes, it might be a port setting issue: I see that you set the database port to 3308. That is probably the port on which MariaDB is listenning. Try to determine if there is another db running on your server listening to another port (for example 3306 is very often the default MySQL port). To do so, on a Linux server you could try the following command :
netstat -tlnp | grep mysql
If you find only x x.x.x.x:3308 address or so, I bet that MySQL is not running on your server and you only run a MariaDB instance. But if you find addresses on other ports (for example x x.x.x.x:3306) chances are that a MySQL instance is running and listens that other port. If that's the case, you should consequently change the DATABASE_URL in your .env file.
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.
Just before i get started, I have been trying to figure this out and have clicked nearly every google link there is and I have read nearly all the other questions on this. But I'm stuck because the bundles that are being suggested for this are out of date.
I am creating a website. Tt is a symfony2 application that is being hosted on Microsoft azure. What I want to do is be able to use doctrine of course to fetch and create users to and from the database.
Now from what I've been reading, to connect to this type of database, I have to use the driver called "PDO_dblib".
I have installed this bundle as it seems to be the only one thats still active, don't quote me on that.
https://github.com/realestateconz/MssqlBundle
Now, I installed this in my vendor folder, is this the correct place to store it? Like so:
Project/
app/
src/
vendor/
realestate/
Of course i added the bundle to the AppKernel like so:
new Realestate\MssqlBundle\RealestateMssqlBundle();
and last but not least here is what i have in my config file:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
dbname: %database_prefix%%database_name%
user: %database_user%
password: %database_password%
so what im thinking im doing here is telling doctrine to use this driver? dont see what else it could be.
I have also declared my parameters.yml for the connection settings.
PS: I am doing my Dev on Linux Mint!
before i tried the steps of this bundle i also ran throught this websites steps: https://dunglas.fr/2014/01/connection-to-a-ms-sql-server-from-symfony-doctrine-on-mac-or-linux/
But again it was throwing errors, I'll post the errors I get down below!
So for the record I have installed freetds and php5-sybase.
The errors that I am getting are such:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_implements(): Class Realestate\MssqlBundle\Driver\PDODlib\Driver does not exist and could not be loaded
and also this when i try to do a:
php app/console doctrine:database:create
(I do have my entity set up)
but i get the following error from the command of create:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
I have been trying to get this working for the past few days and any help would be fantastic! Any more information needed feel free to ask of course!
According your error code:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
It seems that you miss configuring the parameter “database_prefix”.
If the tables in your database have the unitized prefix e.g. “core_user”,”core_tasks”… You can configure this parameter in the file parameters.yml if not, you can just remove the parameter % database_prefix % in the dbname line in file config.yml.
Here are my code snippets for your reference:
Config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
parameters.yml:
parameters:
database_host: {your_sql_server_name}.database.windows.net
database_port: 1433
database_name: {database_name}
database_user: {username}
database_password: {password}
And the test query in controller:
$conn = $this->get('database_connection');
$data = $conn->fetchAll('SELECT * FROM Testtable');
var_dump($data);