Multiple db connections symfony - symfony

I work with multiple db connections
should i use multiple entity managers ?, and what can i do with mapping ?
i use the same entity folder but i need to connect to many DBs.
This is the file : config/packages/doctrine.yaml dbal
doctrine:
dbal:
default_connection: default
connections:
default:
server_version: "%server_version%"
driver: pdo_mysql
url: '%env(resolve:DATABASE_URL)%'
slave:
server_version: "%server_version%"
driver: pdo_mysql
url: '%env(resolve:DATABASE_SLAVE_URL)%'
orm part
orm:
auto_generate_proxy_classes: false
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
slave:
connection: default

Related

Doctrine.yaml" does not contain valid YAML: A colon cannot be used in an unquoted mapping value

I have seen similair questions being asked but did not find my answer.
Most were typo's of some kind and that could be true for me but ive been staring at this for an hour now and cannot find one, maybe someone else can I would really appreciate it!
doctrine:
dbal:
default_connection: default
connections:
default:
#User DB
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
mededelingen:
#mededelingen DB
url: '%env(resolve:DATABASE_MEDEDELINGEN_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
default:
connection: default
mappings:
users:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity\User'
alias: App
mededelingen:
connection: mededelingen
mappings:
Mededelingen:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity\Mededeling'
alias: Mededeling
line 41 (near " alias: Mededeling")
I tried to fix your issue using my linter.
First, you missed the property entity_managers after orm, then I had to move naming_strategy and auto_mapping
auto_mapping can only be used on one entity manager so I set it on default.
The result which cause no issue for me:
doctrine:
dbal:
default_connection: default
connections:
default:
#User DB
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
mededelingen:
#mededelingen DB
url: '%env(resolve:DATABASE_MEDEDELINGEN_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
entity_managers:
auto_generate_proxy_classes: true
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
users:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity\User'
alias: App
mededelingen:
connection: mededelingen
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Mededelingen:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity\Mededeling'
alias: Mededeling

Symfony4 work with multiple entity Manager and connection

I'm working on an App which is built with Symfony and twig.App is connected to two database and have two entity manager as you can see in my configuration below.
When I try to used the default entity manager called (neobe) in my case on (APP\Entity\NeoBe) everything work good, but I can't use another entity manager (neobesante) on (APP\Entity\NeoBeSante)for processing data from entity. Below is the snippet code:
Where am I wrong?
Doctrine.yaml config:
doctrine:
dbal:
default_connection: neobe
connections:
neobe:
# configure these for your database server
url: '%env(resolve:DATABASE_NEOBE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
mapping_types:
enum: string
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
neobesante:
# configure these for your database server
url: '%env(resolve:DATABASE_NEOBESANTE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
mapping_types:
enum: string
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ciController\PropertyController
orm:
default_entity_manager: neobe
entity_managers:
neobe:
connection: neobe
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
NeoBe:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/NeoBe'
prefix: 'App\Entity\NeoBe'
alias: NeoBe
neobesante:
connection: neobesante
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
NeoBeSante:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/NeoBeSante'
prefix: 'App\Entity\NeoBeSante'
alias: NeoBeSante
Here is my controller:
$customerNeoBeRepo = $this->getDoctrine()->getRepository(Customer::class,'neobe')->findOneBy(["email" => $userEmail]);
$customerNeoBeSanteRepo = $this->getDoctrine()->getRepository(Customer::class,'neobesante')->findOneBy(["email" => $userEmail]);

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

Payum Bundle Configuration

I am struggling to define entity mapper found here:
https://github.com/Payum/PayumBundle/blob/master/Resources/doc/capture_funds_with_paypal_express_checkout.md#2-a-configure-doctrine-storage
payum:
contexts:
your_context_name:
doctrine_storage:
driver: orm
model_class: AcmeDemoBundle\Entity\PaypalPaymentInstruction
doctrine:
orm:
entity_managers:
default:
mappings:
payum_paypal_express_checkout_nvp:
is_bundle: false
type: xml
dir: %kernel.root_dir%/../vendor/payum/paypal-express-checkout-nvp/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping
prefix: Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity
My current doctrine configuration is having an autoload to true. Problem I have is to make this mapping work with my autoload: true
I can get mapping to work when i remove my autoload: true but
Any tips would be much appreciated, but question is how do i create a table from this mapping information?
This config works well for me. It added an id feild(it is come from bundle and was automapped) and the rest field come from manually defined mapping.
I added an example to sandbox. It works fine.
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
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
mappings:
payum_paypal_express_checkout_nvp:
is_bundle: false
type: xml
dir: %kernel.root_dir%/../vendor/payum/paypal-express-checkout-nvp/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping
prefix: Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity

Mapping with multiple databases in Doctrine

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

Resources