I have setup a new project with multiple entity managers, when I try to load the data fixtures I get an MappingException as the console tries to load Fixtures for everything rather than the entity manager I specified.
Here is the doctrine section from my config.yml
doctrine:
dbal:
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
symblog:
driver: %database_driver_blog%
host: %database_host_blog%
port: %database_port_blog%
dbname: %database_name_blog%
user: %database_user_blog%
password: %database_password_blog%
charset: %database_charset_blog%
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
connection: default
mappings:
IncompassAuthBundle: ~
IncompassUserBundle: ~
IncompassApiBundle: ~
IncompassSurgeryBundle: ~
IncompassVendorBundle: ~
IncompassHospitalBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
symblog:
connection: symblog
mappings:
IncompassBlogBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
As you can see I've setup a separate connection and entity manager for the symblog tutorial stuff.
When I try
php app/console doctrine:fixtures:load --em=default
I get this
Careful, database will be purged. Do you want to continue Y/N ?Y
> purging database
> loading [1] Incompass\BlogBundle\DataFixtures\ORM\BlogFixtures
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Incompass\BlogBundle\Entity\Blog' was not found in the chain configured namespaces Incompass\AuthBundle\Entity, Incompass\UserBundle\Entity, Incompass\
SurgeryBundle\Entity, Incompass\VendorBundle\Entity, Incompass\HospitalBundle\Entity, FOS\UserBundle\Model
doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."] [--purge-with-truncate]
When I try
php app/console doctrine:fixtures:load --em=symblog
I get
Careful, database will be purged. Do you want to continue Y/N ?Y
> purging database
> loading [1] Incompass\BlogBundle\DataFixtures\ORM\BlogFixtures
> loading [1] Incompass\SurgeryBundle\DataFixtures\ORM\SurgeryStatusFixtures
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Incompass\SurgeryBundle\Entity\SurgeryStatus' was not found in the chain configured namespaces Incompass\BlogBundle\Entity
doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."] [--purge-with-truncate]
So the console command appears to be ignoring the "--em=foobar" option and is trying to load up all the data fixtures it finds.
How can I get doctrine:fixtures:load to only use the specified entity manager?
After Phils comment I moved all my Fixtures into a FixturesBundle and did this
php app/console doctrine:fixtures:load --fixtures=src/Incompass/FixturesBundle/DataFixtures/ORM
For the Blog Fixtures I also had to specify the entity manager
php app/console doctrine:fixtures:load --fixtures=src/Incompass/BlogBundle/DataFixtures/ORM --em=symblog
Related
I used this documentation : https://symfony.com/doc/4.4/doctrine/multiple_entity_managers.html
So now I can create new databases named legacy and Project like this
doctrince.yaml:
doctrine:
dbal:
default_connection: legacy
connections:
legacy:
driver: pdo_mysql
host: "%env(database_host)%"
port: "%env(database_port)%"
dbname: "%env(database_name)%"
user: "%env(database_user)%"
password: "%env(database_password)%"
charset: UTF8
project:
driver: pdo_mysql
host: "%env(database_host_project)%"
port: "%env(database_port_project)%"
dbname: "%env(database_name_project)%"
user: "%env(database_user_project)%"
password: "%env(database_password_project)%"
charset: UTF8
orm:
default_entity_manager: legacy
entity_managers:
legacy:
connection: legacy
mappings:
Legacy:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/..."
prefix: '...'
alias: Legacy
project:
connection: project
auto_mapping: true
mappings:
Project:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity"
prefix: 'App\Entity'
alias: Project
Now I have several Fixture classes all of them depend on each other and also some will create fixture for legacy and some will create for project.
Now my question is when I do:
php bin/console doctrine:fixtures:load --em=legacy
It runs the appfixtures of the project but not the of the bundle.
and then i get this error:
The class .. was not found in the chain configured namespaces ....
My question is now how can I load fixtures on multiple databases with multiple connections.
Thanks in advance.
You should read this doc : https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
I think it may solve your problem as this is exactly what you trying to do.
If you have to create a fixture for legacy just use :
$entityManager = $this->getDoctrine()->getManager('legacy');`
else if it is for project use
$entityManager = $this->getDoctrine()->getManager('project');`
Also, if you change doctrine.dbal.default_connection, entityManager will run against the defined connection.
So you can try to create group of fixtures.
And then run fixtures for project :
Edit config for test environment
#config/packages/test/doctrine.yaml
doctrine:
dbal:
default_connection: 'project'
Then run fixture for 'project' group
> php bin/console doctrine:fixtures:load --group=project
Then do the same thing for 'legacy'
I have an existing entity in a Symfony3 project that I am trying to add an entity manager to. My doctrine.yml file looks like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
lego:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
lego:
connection: lego
mappings:
AppBundle: ~
However anytime I try to access this entity manager through php bin/console doctrine:database:create --connection=lego
, it says the manager does not exist! I'm not sure if I'm missing a step - the only thing I've done to create the manager is to make the yml file.
It doesn't show up when I run bin/console debug:container.
Grateful for any help!
1) You are using the same schema name (database name) on the same host in two different entity managers without schema_filters. This means that when you do an update to one of the entity managers it will attempt to delete the data of the other entity manager.
2) You are asking about a entity manager but in the command you are passing a connection.
3) Run php bin/console debug:container | grep doctrine
This will give you all the doctrine services. Update your question accordingly.
4) Provide the exact command and exact error you get so we can track it down to the origin
5) For a more information run the command in verbose mode adding -v at the end
6) Are you importing your doctrine.yml in your config.yml ?
After searching and debbuging I figured out that doctrine is not loading my entities namespaces. I will show you my project config files
AppKernel.php
new CEOC\PresentacionOfertaBundle\CEOCPresentacionOfertaBundle(),
new CEOC\ContactoBundle\CEOCContactoBundle(),
new CEOC\CoreBundle\CEOCCoreBundle(),
config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_pgsql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
I have one entity under CEOCContactoBundle (CEOCContactoBundle:Organismo) created by doctrine:generate:entity(it worked fine!) then I did the crud with doctrine:generate:crud(it worked fine!!) but when I'm accessing I got an ORMException: Unknown Entity namespace alias 'CEOCContactoBundle' when I'm calling the EntityManager.
After that I found that the array $this->_attributes['entityNamespaces'] from Configuration.php under ORM was empty.
Why Doctrine recognize my entities namespace when I created them, when I did the crud but it does not when I using the entity manager?!!
I created my project following these steps:
composer create-project symfony/framework-standard-edition
composer update
Thanks in advance.
I tried to create an entity from a table in a pre-existing database :
php bin/console doctrine:mapping:import --force AppBundle yml --filter="someTable"
But a message showed up stating :
Table someTable has no primary key. Doctrine does not support reverse
engineering from tables that don't have a primary key.
I checked, and in the DB, there were some tables without any PK (and I cannot change that).
So I read this page : http://symfony.com/doc/current/doctrine/multiple_entity_managers.html
... and tried this solution on github :
https://github.com/doctrine/DoctrineBundle/issues/441
My config.yml is like this :
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
forMapping:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
schema_filter: someTable
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle:
type: yml
dir: Resources/config
forMapping:
connection: forMapping
mappings:
AppBundle:
type: yml
dir: Resources/config
But when I try :
php bin/console doctrine:mapping:import --em=forMapping --filter="someTable" AppBundle yml
I get :
[InvalidArgumentException] Doctrine ORM Manager named "forMapping" does not exist.
Emptied the cache, but nope. Any help appreciated.
MC
Sorry, I goofed on my previous answer.
How about you try the command like this instead:
php bin/console doctrine:mapping:import "AppBundle" yml --em=forMapping --filter="someTable"
Does it make a difference? I see from a command line using the --help, it show in that order.
[Solved] Was due to the "auto_mapping". Correct sequence seems to be :
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
AppBundle:
type: yml
dir: Resources/config
# See https://github.com/doctrine/DoctrineBundle/issues/441
forMapping:
connection: forMapping
mappings:
AppBundle:
type: yml
dir: Resources/config
It works one step further to the next error message. When I try :
php bin/console doctrine:mapping:import "AppBundle" yml --em=forMapping
The following error message pops up :
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: preg_match(): Delimiter must not be alphanumeric or backslash
Ok, let's see to this. The schema_filter was wrong. Correct code is :
schema_filter: ~^spip_rubriques$~
My entity has been correctly generated. I thank you all for your help.
MC
It looks like your "forMapping" is not correct in
orm:
entity_managers:
#problem here
forMapping:
Try to find out what is going wrong here
We are retro-engineering a web application using a postgres database. We want to refactor the code using the symfony framework but we are facing a problem to use the database with it.
For the moment our project is working with a MySQL database just using the same structure for the used tables. The problem is that, now, we need to use the postgresql database because there is a lot of data and it's not really adapted to MySQL as far as we know.
We made a lot of research but we didn't succeed to create a postgresql database in the symfony project.
First, we tried to apply this tutorial : http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql we adapted it as much as possible for our project.
Here is our config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
#Mysql
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
#Postgresql
pgsql:
driver: pdo_pgsql
host: localhost
port: 5432
dbname: "%psql_database_name%"
user: root
password: "%psql_database_password%"
charset: UTF8
#mapping_types:
#geometry: string
orm:
default_entity_manager: default
auto_generate_proxy_classes: "%kernel.debug%"
#naming_strategy: doctrine.orm.naming_strategy.underscore
#auto_mapping: true
entity_managers:
default:
connection: default
# lister les Bundles utilisant la connexion par defaut
#mappings:
#monprojetmysqlBundle: ~
#tutoUserBundle: ~
pgsql:
connection: pgsql # connection name for your additional DB
# bundles utilisant la connexion Postgresql
#mappings:
# PostgresqlBundle: ~
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
but when we launched this command : php bin/console doctrine:database:create --connection=pgsql we had this answer :
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: could not find driver
[Doctrine\DBAL\Driver\PDOException]
could not find driver
[PDOException]
could not find driver
doctrine:database:create [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
It seems that we don't have the module pdo_pgsql so we searched how to install it.
To do it, we applied the script proposed on this github page : https://gist.github.com/doole/8651341#file-install_psql_php-sh
We changed the version of postgres.app to 9.5.
After a few tries, we finally succeeded to have pdo_pgsql on the result of php -m.
But know, we have this answer when we launch the command : php bin/console doctrine:database:create --connection=pgsql
PHP Warning: PHP Startup: pdo_pgsql: Unable to initialize module
Module compiled with module API=20131226
PHP compiled with module API=20121212
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pgsql: Unable to initialize module
Module compiled with module API=20131226
PHP compiled with module API=20121212
These options need to match
in Unknown on line 0
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: could not find driver
[Doctrine\DBAL\Driver\PDOException]
could not find driver
[PDOException]
could not find driver
doctrine:database:create [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
We tried to do this : Install PHP with Postgresql on MAC using homebrew
but it didn't change anything. Now we have 5 PHP WARNING for pdo_pgsql and pgsql when we launch the command php bin/console doctrine:database:create --connection=pgsql
We have also seen this :
How to change a database to postgresql with Symfony 2.0? and this : How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2 but it didn't really help because the first one concern debian and we are working on OS X El Capitan and the second one don't tell more than the previous tutorial.
Finally, the only hope we have is that someone can help us...
Thank you in advance.
Finally, I fixed it by removing all the files that manage php and reinstalling php with homebrew.
---- Removing php ----
First, I used the following command from root (cd /) to find all the files starting by "php"
find . -name "php*"
Depending on the results (you might have a lot), remove all the files that need to be removed (at this point it's your judgement that matter). For example, I removed files in /usr/local and /usr/bin but not in /Applications or /Homebrew.
Examples :
rm -Rf /usr/bin/php*
rm -Rf /usr/local/php*
Sometimes, you can have a "permission denied" error even with sudo but it didn't make problem at the end.
---- Reinstalling php ----
Once everything concerning php is removed, you can reinstall it using the following command line :
brew install php56 --with-postgresql
If you have a "libz not found" error, you will need to launch the following command :
xcode-select --install
and relaunch the installation with :
brew reinstall php56 --with-postgresql
If everything went well, you will only have to define the field date.timezone in php.ini and you will have new php system. You can check that you have the pdo_pgsql module installed using this commande line : php -m.
---- Connect your database to your symfony project ----
First, you need to modify the file app/config/parameters.yml in your project by adding the following code :
# Postgresl
psql_database_driver: pdo_pgsql
psql_database_host: 127.0.0.1
psql_database_port: 5432
psql_database_name: your_database_name
psql_database_user: your_user_name
psql_database_password: your_password
The fields host and port can be different but this two are the default values for symfony and a postgres database.
Then, you will have to modify the file app/config/config.yml at the Doctrine Configuration level this way :
# Doctrine Configuration
doctrine:
dbal:
default_connection: pgsql
connections:
#Mysql
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
#Postgresql
pgsql:
driver: pdo_pgsql
host: "%psql_database_host%"
port: "%psql_database_port%"
dbname: "%psql_database_name%"
user: "%psql_database_user%"
password: "%psql_database_password%"
charset: UTF8
#mapping_types:
#geometry: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
This is an example you can adapt as you wish.
Now, you can connect your database to your project with this command line :
php bin/console doctrine:database:create --connection=pgsql
If you already have entities in your src/AppBundle/Entity you can create your tables with :
php bin/console doctrine:schema:update --force
Everything must be alright now. I hope, it will help someone else who faces this kind of problems.