NOTE: Its not a Duplicate issue cause I have tried everything I get on google but nothing have helped us.
I am trying to import Tables using Doctrine Reverse Engineering tool, but I m getting this message:
Database does not have any mapping information.
My Connection Details in Config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: null
dbname: pixel_ashish
user: root
password: abc123
charset: UTF8
schema_filter: ~^(?!some_table1|some_table2)~
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
proxy_namespace: Proxies
resolve_target_entities: []
What I have tried So Far:
Running php app/console doctrine:mapping:import --force AcmeBlogBundle xml
gives same error
Tried to convert mapping also which does not make any sense cause mappings are not there but still tried didn't worked out.
Created a new project and tried above given configuration didn't worked.
Now I am out of ideas please help me to solve this.
You need to create some mapping information in your config.
You can do it by passing auto_mapping: true under orm section:
doctrine:
orm:
auto_mapping: true
or by manually defining it under orm section:
doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundleName:
type: "xml"
dir: "Entity"
prefix: "Your\BundleName\Entity"
Related
On doctrine/doctrine-migrations-bundle 2.* this was relatively simple - use the --em option (and use ContainerAwareInterface to skip any migrations from a different em/connection).
Now (on doctrine/doctrine-migrations-bundle 3.2.2), it seems the --em option is ignored, and the default em/connection is always specified, meaning the migrations for the default em are applied to every database. Edit: As pointed out in comments - --em is not ignored, it's passed through directly, it's rather our ContainerAwareInterface approach that's no longer valid.
There is a lot of conflicting information on how to set this up, some suggesting it should "just work" (Symfony Docs) and other describing workarounds (Issue):
https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
https://github.com/doctrine/DoctrineMigrationsBundle/issues/38
How does one configure this new version (3) of doctrine/doctrine-migrations-bundle to apply migrations only to their matching entity/db?
Edit: I've included below our config previous to upgrading, which along with the ContainerAwareInterface connection filtering approach, allowed filtering migrations to run only against the appropriate entity manager.
Our existing "doctrine/doctrine-bundle": "1.12.8" config (shortened, but shows multiple entity managers):
doctrine:
dbal:
connections:
default:
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
driver: '%database_driver%'
server_version: mariadb-10.4.11
host: '%database_host%'
port: '%database_port%'
dbname: autotempest
user: '%database_user%'
password: '%database_password%'
mapping_types:
enum: string
model:
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
driver: '%database_driver%'
server_version: mariadb-10.4.11
host: '%database_host%'
port: '%database_port%'
dbname: autotempest_models
user: '%database_user%'
password: '%database_password%'
mapping_types:
enum: string
wrapper_class: App\Doctrine\ConnectionWrapper\ConnectionModel
persistent: true
orm:
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: default
mappings:
App:
type: 'annotation'
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
model:
connection: model
mappings:
TempestModelBundle:
type: 'annotation'
dir: 'Entity'
prefix: 'Tempest\Bundle\ModelBundle\Entity'
Our "doctrine/doctrine-migrations-bundle": "2.1.2" config:
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/Migrations'
namespace: Application\Migrations
Also mentioned in my question, there is an open issue on the DoctrineMigrationsBundle from 2012 describing the problem of dealing with migrations when using multiple entity managers: https://github.com/doctrine/DoctrineMigrationsBundle/issues/38. It seems there are several options for workarounds to this issue as described there, we just needed to dig and try each of them to find the best one for our situation.
Container Aware Migrations
On Symfony 3, we were using the ContainerAwareInterface approach. Described in the above issue:
Currently this can be achieved by using Container Aware Migrations. If one can have the service container injected, he can obtain an instance of some entity manager and its connection.
This is no longer really a valid solution when moving to Symfony 4 however, due to ContainerAware classes being deprecated in favor of dependency injection.
Pass configuration directly
Another approach mentioned in the github issue above. The idea here is to have a separate configuration file for each entity manager like the following:
# config/packages/migrations/base.yaml
em: default
transactional: false
migrations_paths:
Hyra\Migrations\Base: src/Migrations/Base
table_storage:
table_name: migration_versions
This is passed directly to the command, along with the entity manager like this: bin/console doctrine:migrations:migrate --em default --configuration config/packages/migrations/base.yaml. These separate config files replace the single config/packages/doctrine_migrations.yaml configuration file.
This was also not viable for us, as we still needed to inject services into our migrations using the services configuration option of DoctrineMigrationsBundle, and --configuration only passes configuration options directly through to doctrine/migrations, which doesn't support the services configuration option.
Initially on DoctrineMigrationsBundle 3.0, this approach was complicated by the fact that the --em and --conn options were dropped completely, so it was also necessary to create a wrapper on top of the DoctrineMigrationsBundle commands to re-implement these options (described in more detail here). This is no longer necessary on DoctrineMigrationsBundle 3.1+ (which restored these options).
Use DoctrineMigrationsMultipleDatabaseBundle
Also mentioned in the github issue thread, this bundle implements what we needed exactly (and ended up using) - per-entity configuration for DoctrineMigrationsBundle, so we can also include our services config for migration dependency injection. Initially I misconfigured this - it's important that the base doctrine_migrations.yaml config only includes config for the default entity manager. Sample working config provided by the package author (version 0.3.3):
# doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
server_version: mariadb-10.1.26
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
mapping_types:
enum: string
geonames:
url: '%env(resolve:GEONAMES_DATABASE_URL)%'
server_version: mariadb-10.1.26
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
mapping_types:
enum: string
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '5.7'
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/App/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
geonames:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
connection: geonames
mappings:
Geonames:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/App/Entity/Geonames'
prefix: 'App\Entity\Geonames'
alias: Geonames
# doctrine_migrations.yaml
doctrine_migrations:
em: default
migrations_paths:
DoctrineMigrations: '%kernel.project_dir%/migrations/Main'
# doctrine_migrations_multiple_database.yaml
doctrine_migrations_multiple_database:
entity_managers:
default:
migrations_paths:
DoctrineMigrations\Main: '%kernel.project_dir%/migrations/Main'
geonames:
migrations_paths:
DoctrineMigrations\Geonames: '%kernel.project_dir%/migrations/Geonames'
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'm about to embark on using RDS with a master read/write and slave read-only setup.
I've read about the Doctrine MasterSlaveConnection type.
However, there will be some endpoints I create that I would like to use the read-only replica (most of my GET endpoints) and some will need to write data (PUT, PATCH, DELETE endpoints).
I'm also using API Platform.
What is the best way of achieving this in Symfony 4 and Doctrine 2?
What I have done in the past is to just use different connections.
Something like:
doctrine:
dbal:
default_connection: default
connections:
default:
# This is your Master
url: '%env(DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
slave:
# This would be the slave
url: '%env(DATABASE_SLAVE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
slave:
connection: slave
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
Then in your controllers or business logic you can either choose to use the default entity manager:
// Controller
$this->getDoctrine()->getEntityManager();
Or you can get the slave connection:
// Controller
$this->getDoctrine()->getEntityManager('slave');
If you need this to work just on all requests without having to create special actions for everything then your best bet is to decorate the Collection and Item DataProviders for doctrine.
https://symfony.com/doc/current/service_container/service_decoration.html
https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/CollectionDataProvider.php
https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/ItemDataProvider.php
So basically you need to change what manager is chosen based on the $opperationName something like:
if($opperationName === 'GET'){
$manager = $this->managerRegistry->getManager('slave');
} else {
$manager = $this->managerRegistry->getManager();
}
You actually don't need to setup multiple entity managers, nor is it preferable as handling one entity with multiple entity managers is hard.
Using Doctrine 2.2, you can setup slaves/replicas directly from configuration without needing an extra entity manager:
See the config reference here:
https://www.doctrine-project.org/projects/doctrine-bundle/en/2.2/configuration.html#configuration-overview
Example:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: '%env(DATABASE_DBNAME)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASSWORD)%'
host: '%env(DATABASE_HOST)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
slaves:
ro_replica:
dbname: '%env(REPLICA_DBNAME)%'
user: '%env(REPLICA_USER)%'
password: '%env(REPLICA_PASSWORD)%'
host: '%env(REPLICA_HOST)%'
charset: utf8mb4
Thank you #Chase for the solution. You have made my day. Although it works for me in 'dev' environment I had a problem when switched to 'prod'. I was getting an error that an Entity can not be found. The solution came from this post - thanks #xabbuh. Basically I had to add default_entity_manager: name_of_default_em to doctrine.yml. Here is the copy of the code:
# config/packages/prod/doctrine.yaml
doctrine:
orm:
default_entity_manager: BOE <- add this line to let know prod about default em
auto_generate_proxy_classes: false
metadata_cache_driver:
type: service
id: doctrine.system_cache_provider
query_cache_driver:
type: service
id: doctrine.system_cache_provider
result_cache_driver:
type: service
id: doctrine.result_cache_provider
# ...
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