Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Assuming that we have Backend and Frontend bundles and each have their own entities. The entities live in different databases so we'll need dedicated entity managers for each. For more info, click here.


Application structure


src
Application
BackendBundle
Entity
User.php
Log.php
Activity.php
FrontendBundle
Entity
Blog.php
Visit.php

Parameters.yml


parameters:
database_host: 127.0.0.1
database_port: null
database_name: symfony_backend
database_user: root
database_password: null

frontend_database_host: 127.0.0.1
frontend_database_port: null
frontend_database_name: symfony_frontend
frontend_database_user: root
frontend_database_password: null

Config.yml


doctrine:
dbal:
default_connection: backend
connections:
backend:
driver: pdo_mysql
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
mapping_types:
enum: string

frontend:
driver: pdo_mysql
host: %frontend_database_host%
port: %frontend_database_port%
dbname: %frontend_database_name%
user: %frontend_database_user%
password: %frontend_database_password%
charset: UTF8
mapping_types:
enum: string

orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: backend
entity_managers:
backend:
connection: backend
mappings:
ApplicationBackendBundle:
dir: Entity

frontend:
connection: frontend
mappings:
ApplicationFrontendBundle:
dir: Entity

Test


After manually creating symfony_backend and symfony_frontend databases, you can use commands below to create entities automatically.


Inanzzz-MBP:football inanzzz$ app/console doctrine:schema:create --em=frontend --env=dev
ATTENTION: This operation should not be executed in a production environment.

Creating database schema...
Database schema created successfully!
Inanzzz-MBP:football inanzzz$ app/console doctrine:schema:create --em=backend --env=dev
ATTENTION: This operation should not be executed in a production environment.

Creating database schema...
Database schema created successfully!

Accessing it in application


Dependency Injection


You can use lines below as service arguments in services.yml file and reference to use Doctrine\ORM\EntityManager; in your class.


@doctrine.orm.backend_entity_manager
@doctrine.orm.frontend_entity_manager

Controller


# Just getting entity managers
class TestController extends Controller
{
public function indexAction()
{
// All three return the "backend" entity manager
$em = $this->get('doctrine')->getManager();
$em = $this->get('doctrine')->getManager('default');
$em = $this->get('doctrine.orm.backend_entity_manager');

// Both of these return the "frontend" entity manager
$em = $this->get('doctrine')->getManager('frontend');
$em = $this->get('doctrine.orm.frontend_entity_manager');
}
}

# Accessing repositories through entity managers
class TestController extends Controller
{
public function indexAction()
{
// Retrieves a repository managed by the "backend" em
$result = $this->get('doctrine')
->getRepository('ApplicationBackendBundle:User')
->findAll()
;

// Explicit way to deal with the "backend" em
$result = $this->get('doctrine')
->getRepository('ApplicationBackendBundle:User', 'backend')
->findAll()
;

// Retrieves a repository managed by the "frontend" em
$result = $this->get('doctrine')
->getRepository('ApplicationFrontendBundle:Blog', 'frontend')
->findAll()
;
}
}

FeatureContext


$this->kernel->getContainer()->get('doctrine.orm.backend_entity_manager');
$this->container->get('doctrine.orm.backend_entity_manager');