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
Related
I'm tring to use postgres whi symfony 5 and php 7 on windows 10 .
in my env file :
DATABASE_URL="postgresql://postgres:1234#127.0.0.1:5432/dbtest?serverVersion=12&charset=utf8"
I get could not find driver .
currelty in my php.ini file :
...
extension=pdo_pgsql
extension=pdo_sqlite
extension=pgsql
so i tried this database url
DATABASE_URL="pdo_pgsql://postgres:1234#127.0.0.1:5432/dbtest?serverVersion=12&charset=utf8"
doctrine.yaml conf :
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# 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
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Ap
after : php bin/console d:d:c
i get :
i don't understand this behavior
The database url is taken as a database name in phpmyadmin
The database should be a postgres database so why i see the dabase under phpmyadin ?
I will be really gratful if some explain this to me !
Im having this weird issue with doctrine: Im working on an old project using Symfony (3.4.49)
I have to create a new table on its database so I also created a new Entity class, called App\Entity\PayrollPayRulesDefault, on src/Entity, with all other entities
On my local environment, it just worked. But when I pushed it to the staging server to test, it just wont:
request.CRITICAL: Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'App\Entity\PayrollPayRulesDefault' was not found in the chain configured namespaces " at /var/app/current/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 {"exception":"[object] (Doctrine\\Persistence\\Mapping\\MappingException(code: 0): The class 'App\\Entity\\PayrollPayRulesDefault' was not found in the chain configured namespaces at /var/app/current/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23)"} []
Doctrine config is exactly the same for both envirmoments:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: project
connections:
project:
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
messagequeue:
# configure these for your database server
url: '%env(resolve:MESSAGING_QUEUE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: project
entity_managers:
project:
connection: project
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
messagequeue:
connection: messagequeue
mappings:
MessageQueue:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/MessageQueue'
prefix: 'App\Entity\MessageQueue'
alias: MessageQueue
The entity class itself seems correct:
namespace App\Entity;
use App\Entity\Component\EntityComponent;
use Doctrine\ORM\Mapping as ORM;
/**
* PayrollPayRulesDefault
*
* #ORM\Table(name="payroll_pay_rules_default", indexes={#ORM\Index(name="ruleSubType", columns={"ruleSubType"})})
* #ORM\Entity
*/
class PayrollPayRulesDefault extends EntityComponent
{
I tried clearing the cache:
php bin/console cache:clear
php bin/console doctrine:cache:clear-metadata
Tried warming it up too:
php bin/console cache:warmup
Also tried just deleting the cache directory too: nothing changes
Im dont have a lot of experience with symfony, but, from what I can tell, the project configuration seems correct. And is the same on my local server, where it works
So I dont know what to try or investigate anymore. Please help, this is driving me crazy
Alright, I was able to fix it myself finally.
php bin/console cache:clear --env=prod
php bin/console doctrine:cache:clear-metadata --env=prod
php bin/console cache:warmup --env=prod --no-debug
php bin/console doctrine:mapping:info --env=prod
I'm working on a Symfony project, using doctrine as ORM with MariaDB as a driver.
To test my different job, I'm working with gitlab-runner in local on MacOs.
Problem
I create a job to test the creation of my different databases and the migrations for both.
I'm using the symfony-cli command line to create my databases from the doctrine's file configuration (see in the section Source File)
The command line to create a database with symfony-cli
$ php bin/console doctrine:database:create --env=test --if-not-exists --connection=[CONNECTION_NAME]
The error prompt when the command line from is asked to run
$ php bin/console doctrine:database:create --env=test --if-not-exists --connection=CleanerFuture
Could not create database `cf_CleanerFuture` for connection named CleanerFuture
An exception occurred while executing 'CREATE DATABASE `cf_CleanerFuture`':
SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'myapptest'#'%' to database 'cf_CleanerFuture'
Goal
Well my goal it's to try to create the database base with this command line to run my job and past to the next one
Source File
.gitlab-ci.yml
This is the job I try to execute
doctrine-migrations:
image: php:7.3
stage: Migrations
services:
- name: mysql:5.7
alias: mysql
variables:
ENV: test
MYSQL_ROOT_PASSWORD: pass_test
MYSQL_DATABASE: cf_Central
MYSQL_USER: myapptest
MYSQL_PASSWORD: myapptest
DATABASE_URL: 'mysql://myapptest:myapptest#mysql:3306/'
before_script:
- apt-get update
- apt-get install -y git libzip-dev
- curl -sSk https://getcomposer.org/installer | php -- --disable-tls && mv composer.phar /usr/local/bin/composer
- docker-php-ext-install mysqli pdo pdo_mysql zip
- curl -sS https://get.symfony.com/cli/installer | bash
- mv /root/.symfony/bin/symfony /usr/local/bin/symfony
- composer remove ext-xdebug
- composer install
script:
- php bin/console doctrine:database:drop --force --if-exists --env=test --connection=default
- php bin/console doctrine:database:drop --force --if-exists --env=test --connection=CleanerFuture
- php bin/console doctrine:database:create --env=test --if-not-exists --connection=CleanerFuture
- php bin/console doctrine:migrations:migrate --env=test
allow_failure: false
config/test/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%cf_central'
server_version: "mariadb-10.4.11"
driver: 'pdo_mysql'
charset: utf8
CleanerFuture:
url: '%env(resolve:DATABASE_URL)%cf_CleanerFuture'
server_version: "mariadb-10.4.11"
driver: 'pdo_mysql'
charset: utf8
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
connection: default
mappings:
Central:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Central'
prefix: 'App\Entity\Central'
CleanerFuture:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
connection: CleanerFuture
mappings:
Client:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Client'
prefix: 'App\Entity\Client'
url: '%env(resolve:DATABASE_URL)%cf_CleanerFuture' in your config/test/doctrine.yaml file:
The part cf_CleanerFuture will be added to the string value of the variable DATABASE_URL which is defined in your .env file.
If your .env file value for the datbase connection contains something like
DATABASE_URL="mysql://symfony:symfony#database:3306/symfony?serverVersion=8.0"
the processed content of your config will be
doctrine:
dbal:
default_connection: default
connections:
default:
url: 'mysql://symfony:symfony#database:3306/symfony?serverVersion=8.0cf_central'
'mysql://symfony:symfony#database:3306/symfony?serverVersion=8.0cf_central' seems to be valid (doctrine can connect and the parameter serverVersion will be ignored if not usable), but the value of the parameter serverVersion is now 8.0cf_central. That makes no sense, is not a valid version number and you haven't configured a different database connection.
It is more recommended to use two different environment variables and store the whole connection URL in these variables:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
See also the Symfony documentation which describes how to configure multiple database connections: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
Im running symfony with xampp. Trying to connect to and create db trhough doctrine framework. When i write doctrine:database:create im answered with Could not find driver.
But my pdo driver works well. It is present in phpinfo, plus i can easily connect to db through the pdo constructor.
Here is my doctrine configs:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: pdo_mysql
# 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
And my env:
DATABASE_URL="mysql://root:#127.0.0.1:3306/aihara"
check if you don't have another php.ini where the pdo_mysql is disabled
I am trying to setup a second database for a symfony 4.2 project. Everything seems to working fine up until the point where I run migrations where all migrations execute on the given connection instead of only the migrations for the connection it was created.
Following symfony's own documentation about this, my doctrine.yaml looks like this:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(DATABASE_URL)%'
logging:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(DATABASE_URL_LOG)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
default:
connection: default
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
logging:
connection: logging
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
mappings:
Log:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Log'
prefix: 'App\Log'
alias: Log
Now with this configuration I am able to run the migration commands with the --em=log/default parameter like this:
php bin/console doctrine:migrations:diff --em=log
php bin/console doctrine:migrations:migrate --em=log
And you get the expected results: It only creates a new migration for a new entity I created at src/Log when I add --em=log.
this is the new entity:
<?php
namespace App\Log;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class LogItem
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
}
However, the migration is created in the default src/Migrations folder and as a result:
1) this migration is also executed when I run doctrine:migrations:migrate --em=default (creating the table in the default database)
2) the entire default schema is loaded into the log database when I run doctrine:migrations:migrate --em=log
So I think the solution would be to split the migration files between the to entity managers into different directories. But I have been spending hours and can't find the way to do this for symfony4. Also, since the symfony documentation mentions absolutely nothing about this I feel like maybe something is wrong in the way it is setup now.
So can anyone tell me what I am doing wrong here? Or can (and if yes: how) do I split the migration files so it will only execute the migrations created for the given entity manager?
doctrine/doctrine-migrations-bundle doesn't support migrations for multiple entity managers out of the box. There are various workarounds to this problem, including:
Container aware migrations (only feasible on Symfony <4)
Storing migration configuration for each entity manager separately and passing it directly via the --configuration option of the doctrine:migrations:migrate command (only feasible if you don't need to inject services/dependencies into your migrations)
Using avaibooksports/doctrine-migrations-multiple-database-bundle (or rolling your own bundle), which implements support for separate configuration per-entity-manager (in which you can specify a separate directory for each entity's migrations, and inject services via factories as needed).
There's an open GitHub issue (opened in 2012) that describes these approaches in more detail. Also see this answer for a summary.
I think you have a typo in your samples.
In your doctrine.yaml file, the names of your entity managers are "default" and "logging".
But in your command line samples you use the entity manager name "log".
It should work when you change the flag from --em=log to --em=logging.