Symfony3: How to set multiple connections? - php

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.

Related

Doctrine ORM Manager named "" does not exist - syntax error

I think all parameters are set properly but when I run command of
php bin/console doctrine:schema:update --force --em=archive
it throws an error:
Doctrine ORM Manager named "archive" does not exist.
So I think I might have a syntax problem that I cannot find.
My code:
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
archive:
driver: pdo_mysql
host: '%database2_host%'
port: '%database2_port%'
dbname: '%database2_name%'
user: '%database2_user%'
password: '%database2_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Default:
mapping: true
type: annotation
dir: '%kernel.root_dir%/../src/App/Base/Entity'
alias: 'Default'
prefix: 'Default\Base\Entity'
is_bundle: false
Archive:
mapping: true
type: annotation
dir: '%kernel.root_dir%/../src/App/Base/Entity/Archive'
alias: 'Archive'
prefix: 'App\Base\Entity\Archive'
is_bundle: false
Also I set parameters.yml correctly, too.

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

Symfony2 Doctrine ORM Manager named “db2” does not exist

I create YML entity, generate entity and config multiple connections
config.yml
# Doctrine Configuration
doctrine:
dbal:
default_connection: db1
connections:
db1:
driver: '%database_driver%'
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
db2:
driver: '%database_driver2%'
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
my orm.yml
TestBundle\Entity\Test:
type: entity
table: Test
id:
id:
type: integer
nullable: false
options:
unsigned: true
id: true
fields:
name:
type: string
nullable: false
length: 255
options:
fixed: false
lifecycleCallbacks: { }
after clean cache and generate entities, I try to run this code (in command controller)
<?php
...
protected function execute(InputInterface $input, OutputInterface $output)
{
$doctrine = $this->getContainer()->get('doctrine')->getManager('db2');
//both methods cant work :( db2_entity_manager not found
$doctrine = $this->getContainer()->get('doctrine.orm.db2_entity_manager');
$test = $doctrine->getRepository('DionisDataBaseBundle:Test');
}
And I have error:
[InvalidArgumentException]
Doctrine ORM Manager named "db2" does not exist.
change config.yml to
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: db1
entity_managers:
db1:
connection: db1
mappings:
DionisDataBaseBundle: ~
db2:
connection: db2
mappings:
DionisDataBaseBundle: ~
db3:
connection: db2
mappings:
DionisDataBaseBundle: ~
you just declared connections but no entity manager, from one of my projects :
doctrine:
dbal:
default_connection: default
connections:
default:
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
mapping_types:
enum: string
string: string
# schema_filter: ~^(sf_fos_user)~
prod:
host: *****
dbname: ****
user: "%database_user%"
password: "%database_password%"
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
mapping_types:
enum: string
string: string
# schema_filter: ~^(sf_fos_user)~
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
Bundle2: ~
db2:
connection: prod
mappings:
otherBundle: ~
auto_generate_proxy_classes: "%kernel.debug%"

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

Symfony2 : Error with entitymanager configuration

Error : InvalidConfigurationException: Unrecognized options "auto_mapping" under "doctrine.orm"
This is my config.yml file code
# 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
symfonydb:
driver: pdo_mysql
host: localhost
port: null
dbname: symfony
user: root
password: null
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
default_entity_manager: default <<<<<<<<<<
entity_managers:
default:
connection: default
mappings:
AcmeDemoBundle: ~
AcmeUserBundle: ~
symfonydb:
connection: symfonydb
mappings:
FooNewsBundle: ~ <<<<<<<<<
If I remove code which is after auto_mapping: true, then it works fine and not throw any error.
So what is the problem?
Thnaks.
Have a look at this GitHub issue: Problem with installation?
The auto_mapping entry goes under the entity_managers.default node, not on the root of the orm node.

Categories