Working with two entity managers in the same bundle in Symfony2 - php

I'm trying to work with two entity managers for the same bundle. My configuration is like this:
orm:
default_entity_manager: default
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Is there any way to tell which entities belong to which entity manager? It crashes now if I want to work with a table which doesn't belong to the default entity manager.
UPDATE
here is my configuration for the connection:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
default_entity_manager: electra
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~

For using multiple entitymanager in same bundle you have to config mapping options for each entitymanager.
http://symfony.com/doc/current/reference/configuration/doctrine.html
Exemple off config file
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
second:
driver: %database_sqlite_driver%
host: ~
port: ~
dbname: %database_sqlite_shop_name%
path: %database_sqlite_shop_name%
user: ~
password: ~
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundle:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/FirstDb"
#the prefix
prefix: "Your\Bundle\Entity\FirstDb"
shop:
connection: second
mappings:
YourBundle:
type: "annotation"
#here the second path where entity for the connection stand
dir: "Entity/SecondDb"
#the prefix
prefix: "Your\Bundle\Entity\SecondDb"
You can now use console for managing your db with the --em parameter
Ex : update database for shop entitymanager
php app/console doctrine:schema:update --em=shop
Read mapping information from Your\Bundle\Entity\SecondDb
Ex : update database for default entitymanager
php app/console doctrine:schema:update
Read mapping information from Your\Bundle\Entity\FirstDb

Ok. Tried to edit your original post but it's waiting for peer review. Not sure how long that takes. Try changing your config to:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
# Make an explicit connection just for clarity
old_project:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
# Humor me and add these
auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
default_entity_manager: electra
entity_managers:
# Make an explicit old_project em so default does not confuse us
old_project:
connection: old_project
mappings:
XXDemoBundle: ~
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Now completely blow away your cache just to be sure then run:
php app/console doctrine:mapping:info --em electra
php app/console doctrine:mapping:info --em old_project
You should get identical results. I tested this on my system so I'm fairly certain that if you don't then you have some typo somewhere.
So mapping info is working. Next step is to verify that both databases match your entity schema. So do this:
php app/console doctrine:schema:update --em electra --dump-sql
php app/console doctrine:schema:update --em old_project --dump-sql
Neither should produce any output. If one does then it means your database does not match your entities and that needs to be resolved (possibly using the --force option) before queries will work.
Once the databases are in sync then you should probably use doctrine:query:dql and do a test query against both managers. Then go back into your code.
=========================================
It has now been understood that the real goal is to have two entity managers point to the same set of entities but somehow indicate that each entity manager should limit itself to a certain set of those entities. And that is not something the S2 supports out of the box.
You could look through the Doctrine manual and see how it handles the entity metadata and maybe do something with that but that could get complicated.
The only thing that S2 really offers is the ability to bind an entity manager to all the entities in one or more bundles using the mapping attribute. If you wanted to share say three of seven entities from one bundle with another bundle then you would simply recreate those entities in the second bundle. Possibly by extending the class so as to avoid code duplication.
I think you might want to alter your approach a bit. If you have a set of core entities shared with multiple bundles then put those in their own bundle. Each follow on bundle can then add additional entities.

Related

Symfony3 - Switch DB Connection Based on Sub Domain

I need to be able to match a connection / entity manager with a sub domain:
one.domain.com => Should use connection and entity manager one
two.domain.com => Should use connection and entity manager two
But I'm not sure how to best achieve this, what would be the correct way of approaching it in Symfony3.4?
My Doctrine Configuration
doctrine:
dbal:
default_connection: one
connections:
one:
driver: pdo_mysql
etc...
two:
driver: pdo_mysql
etc...
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: one
entity_managers:
one:
connection: one
mappings:
UserBundle: ~
AnotherBundle: ~
YetAnotherBundle: ~
two:
connection: two
mappings:
UserBundle: ~
AnotherBundle: ~

Multiple connections with different entities in same Symfony bundle

I'm developing a new project with Symfony that will have a website and an admin site in the same bundle. I have 2 different connections (one for the data database and one for the admin database) and I wanted to be able to distinguish between entities so for that I have this config.yml:
doctrine:
dbal:
default_connection: project
connections:
project:
driver: pdo_pgsql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
backoffice:
driver: pdo_pgsql
host: '%backoffice_database_host%'
port: '%backoffice_database_port%'
dbname: '%backoffice_database_name%'
user: '%backoffice_database_user%'
password: '%backoffice_database_password%'
charset: UTF8
orm:
default_entity_manager: project
entity_managers:
project:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: project
auto_mapping: true
mappings:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/project"
#the prefix
prefix: "Project\\ProjectBundle\\Entity\\Project"
backoffice:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: backoffice
mappings:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/Backoffice/"
#the prefix
prefix: "Project\\ProjectBundle\\Entity\\Backoffice"
Now I'm trying to configure FOSUserBundle and when I want to update the schema for the backoffice database I get the following error: The class 'Project\ProjectBundle\Entity\Backoffice\User' was not found in the chain configured namespaces Project\ProjectBundle\Entity\Project, FOS\U
serBundle\Model. The FOSUserBundle configuration is:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: backoffice
user_class: Project\ProjectBundle\Entity\Backoffice\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
Is there any way to achieve that? Another solution that I thought of is having a Bundle with only the project database entities, but I don't know if that would be too complex.
Thanks

error The attribute "name" must be set for path "doctrine.orm.entity_managers.db2.mappings" with multiple databases

I have a project that uses two databases. The Doctrine section of Config.yml bundle is setup as follows.
doctrine:
dbal:
default_connection: db1
connections:
db1:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
db2:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: em1
entity_managers:
em1:
connection: db1
mapping:
FirstBundle: ~
em2:
connection: db2
mappings:
SecondBundle: ~
All the parameters are setup in the parameters.yml file correctly.
whenever I run any $ php bin/console commands I get the following error
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The attribute "name" must be set for path "doctrine.orm.entity_managers.em2.mappings".
Are there any settings that I'm missing?
I notice your em1 section is set to mapping instead of mappings. Also, in my working config, I set default_entity_manager to the connection value. So maybe try making these changes:
orm:
default_entity_manager: db1
entity_managers:
em1:
connection: db1
mappings:
FirstBundle: ~
em2:
connection: db2
mappings:
SecondBundle: ~
That may not be the problem, but try it.

Symfony2 : Doctrine - Update table structure on two different entity managers

I have two servers A and B and some mapped entity to Server A and another to server B.
When I execute doctrine:schema:update all tables are created on the B server. Any idea why this might be happening?
Here my config.yml file
# Doctrine Configuration
doctrine:
dbal:
default_connection: A
connections:
A:
driver: pdo_sqlsrv
port: 1433
host: A
dbname: MADB
user:
password:
charset: UTF8
B:
driver: pdo_sqlsrv
host: B
port: 1433
dbname: MADB2
user:
password:
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: B
entity_managers:
labete:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
connection: A
mappings:
HMAdminBundle: ~
HMMainBundle: ~
main:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: B
mappings:
HMProfBundle: ~
HMMainBundle: ~
HMAdminBundle: ~
Your configuration indicates that your default entity manager is B. That's why when you run the doctrine command, tables are generated for B.
When you run doctrine commands, there's usually an entity manager option that you can pass in:
doctrine:schema:update --em=A
You can see all the options by running:
doctrine:schema:update --help

Generate entities using xml mappings using command doctrine:genereate:entities to a different location other than the default location

I am using symfony2 with doctrine.
Here is my parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: ~
database_name: somedb
database_user: root
database_password: ~
Here is my config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
dbname: %database_name%
host: %database_host%
port: %database_port%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: false
mappings:
name:
type: xml
dir: %kernel.root_dir%/../src/bundlename/Resources/config/doctrine
prefix: bundlename\Model\Entity
is_bundle: false
Running doctrine:mapping:import generates the xml mappings files in the following directory
*bundlename\Resources\config\doctrine*
Running doctrine:generate:entities gives the following error:
Generating entities for bundle "bundlename"
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'bundlename.Model.Entity.some_entity
.orm.xml' for class 'bundlename\Model\Entity\some_entity'
.
Using auto_mapping: true correctly generates the entities at bundlename\Entity*
What I want to accomplish here is to instruct doctrine to generate entities at a different location other than the default location

Categories