Mapping with multiple databases in Doctrine - symfony

I am trying to set up my symfony to use two databases connections. The problem is that Symfony won't map the Entities to the right EntityManager. So perform a query, I have to tell Symfony what manager it should use.
My config:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
lookup:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "Lookup"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes
So, to query from my Lookup, i have to do: $this->getDoctrine()->getRepository('MbLookupBundle:Country', 'lookup');
Instead, I was hoping I could leave out the second parameter. This way, my bundle would be independent. The project manager can decide what database config he will implement in his project. He'll just have to make sure that the mapping is correct.
How does this work? What are the mappings used for, if this is not possible?

You have mistakes in connections naming :
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
# ...
mvibes: #This is the name of the connection
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes #must refre to a connection's name defined above
Official doc : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
A blog post I wrote about it : http://blog.alterphp.com/2011/10/configuration-trick-for-multiple-entity.html

Related

Symfony wrong persist database working with two entity managers

I have the same symfony app deployed in two servers.
I use two entity managers, one called defaul with a connection on "local" database and the other called online on the "remote" database
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
online:
driver: pdo_mysql
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
dql:
string_functions:
GROUP_CONCAT: AppBundle\DQL\GroupConcatFunction
connection: default
mappings:
LexikTranslationBundle: ~
AppBundle: ~
LilWorksStoreBundle: ~
online:
connection: online
mappings:
AppBundle: ~
LilWorksStoreBundle: ~
I copy some entities from the remote database to the local app within a service. My service get the two entity managers
services:
app.syncro:
class: AppBundle\Service\Syncro
arguments: ['#doctrine.orm.default_entity_manager','#doctrine.orm.online_entity_manager']
I clone the remote entity and I try to persist it in my local
$user = $this->emRemote->getRepository("AppBundle:User")->find(1);
$clonedUser = clone $user;
$this->emLocal->persist($clonedUser->cloneUser());
$this->emLocal->flush();
The flush don't persist in the local database but try to persist in the distant.
Is something in my configuration doesn't works? Maybe my entity still have the remote database name stored in proxy?

Symony: Unknown Entity namespace alias while working with 2 entity managers

I am trying to work with 2 DBs and then 2 entity managers.
Here is my config doctrine part:
doctrine:
dbal:
default_connection: g2s
connections:
main:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
api:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: api
user: '%database_user%'
password: '%database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: main
auto_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore
api:
connection: api
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ApiBundle: ~
I ran the command "config:dump-reference" and the bundle is mapped.
But when trying to browse any page I got this error:
Unknown Entity namespace alias 'ApiBundle'
According to docs, the proper way is to explicitly set all mappings, and this is what I do in my project as well. https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
There was a thread about this issue, and it should work for Symfony 2.6+ but same as #Cerad, I had no luck in enabling this. https://github.com/doctrine/DoctrineBundle/issues/60

How to have DBAL's setSQLLogger in symfony

I was able to connect to doctrine in symfony2, this way:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: false
proxy_namespace: Proxies
proxy_dir: Proxies
default_entity_manager: default # The first defined is used if not set
entity_managers:
default:
connection: default
mappings: # Required
ApplicationUserBundle:
type: annotation
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
dql:
datetime_functions:
UNIX_TIMESTAMP: DoctrineExtensions\Query\Mysql\UnixTimestamp
Now my question is that how to have this in configuration above?
$config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger());
I mean another way than this. As this is a $config method I guess that should be possible to have it in config.yml, but how?
You need to fill few config parameters
In doctrine.yaml config file enable logging
doctrine:
dbal:
logging: true
In services.yaml config file set logger class
services:
doctrine.dbal.logger:
class: Doctrine\DBAL\Logging\EchoSQLLogger
Do not forget to clean Symfony cache before usage php bin/console cache:clear

configuring multiple entity managers in symfony2 that extend 3rd party entities

I'm trying to use an Xhprof bundle in conjunction with XhGUI. I've have two separate databases for my app, one for the actual domain models(postgres) and one to store the profiling data(mysql).
I've created two connections and entity managers in the my config.yml to account for this. I'm having trouble with the mapping now though because two of my bundles have entities that extend entities in 3rd party bundles. The configurations/annotations found in the base classes isn't being picked up by doctrine though. The 3rd party bundles use annotations for the orm configuration so in my classes that extend those, i chose annotation as well.
MyCompanyAuthBundle has entities that extend entities in the FOSOAuthServerBundle bundle. MyCompanyProfileBundle has one entity that extends an entity in the JnsXhprofBundle bundle.
Now that i'm having to manually configure the mappings sections in my config.yml, i can't figure out how to tell doctrine to also use the entities base class annotation configuration found in the 3rd party bundles.
Before i started trying to use two connections, i just used the auto_mapping flag in the orm config and it worked great with my single connection.
Anyone know what i'm doing wrong here?
Here is the doctrine part of my config.yml.
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: %database_name%_%kernel.environment%
user: "%database_user%"
password: "%database_password%"
charset: UTF8
xhprof:
driver: "%database_driver2%"
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyCompanyCoreBundle: ~
MyCompanyAuthBundle:
type: annotation
xhprof:
connection: xhprof
mappings:
MyCompanyProfileBundle: ~
JnsXhprofBundle: ~
Figured it out. Easier than i expected!
Just needed to add the bundles that also have mapping.
My working config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: %database_name%_%kernel.environment%
user: "%database_user%"
password: "%database_password%"
charset: UTF8
xhprof:
driver: "%database_driver2%"
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyCompanyCoreBundle: ~
MyCompanyAuthBundle: ~
FOSOAuthServerBundle: ~
xhprof:
connection: xhprof
mappings:
MyCompanyProfileBundle: ~
JnsXhprofBundle: ~

Shared connection : User is not a valid entity or mapped super class

I had 5 minutes ago a full setup with FOS userBundle which was working. Then i decided to use multiple / shared db connections. And now i've got :
MyNamespace\UserBundle\Entity\User is not a valid entity or mapped super class.
Here's the setting.
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
shared:
driver: %database_shared_driver%
host: %database_shared_host%
port: %database_shared_port%
dbname: %database_shared_name%
user: %database_shared_user%
password: %database_shared_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyNonSharedBundle: ~
shared:
connection: shared
mappings:
MyUserBundle: ~
FOSUserBundle: ~
PS: If i Move MyUserBundle and FosUserBundle under the default connection, it works.
That means something is trying to work with your entity through default entity manager. Did you configure FOSUserBundle?
fos_user:
model_manager_name: "shared"

Resources