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

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

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: ~

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.

Symfony3: How to set multiple connections?

I'm working with a Symfony3 app and I want to set up multiple connections to different databases.
I've been looking around and I found out the documentation about entityManagers and DB connections. My config.yml is configured as follows:
config.yml
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
other:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
mapping_types:
enum: string
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
So now I do can access to my database like this:
$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');
But what I really need is to configure a second orm-mapping connection which will allow me to interact with entities instead of dealing with the second database directly. So again as the documentation says I added under the doctrine orm label:
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
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: ~
other:
connection: other
mappings:
OtherBundle: ~
This throws an exception:
ParseException in Parser.php line 296:
Unable to parse at line 78 (near " entity_managers:").
How should I configure my config.yml to allow orm-mapping for my second database connection? Should I delete the dql label and use it only under a certain entity manager label?
check the doc here for full reference of the configuration about doctrine.
Check the indentation of the dql function etc.
Probably us are using the Shortened Configuration Syntax where all options available can be placed directly under doctrine.orm config level.
Hope this help
Try this one:
doctrine:
orm:
auto_generate_proxy_classes: true
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
other:
mappings:
OtherBundle: ~
I solved the problem. Thanks for the answers because they were pretty much the solution. I just had to use the labels in their correct levels.
Just in case it can be useful for someone, I'll post what I've done:
config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
other:
mappings:
OtherBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
Once the (other) entity manager is created you just can use it within a controller as you'd do with the default em just by specifying the entity manager name you are using.
$orders = $this->get('doctrine')
->getRepository('OtherBundle:Orders', 'other')
->findAll();
Now you're ready to go.
Thanks for your 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

Working with two entity managers in the same bundle in Symfony2

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.

Categories