10/07/2015 - DOCTRINE, SYMFONY
Varsayalım ki elinizde Backend ve Frontend olmak üzere iki farklı bundle var. Her ikisininde kendilerine mahsus entitileri var ve bu entitiler farklı veritabanlarında tutuluyor. Bu gibi durumlar için ayrı entity managerlerine ihtiyacımız var. Daha fazla bilgi için burayı tıklayın.
src
Application
BackendBundle
Entity
User.php
Log.php
Activity.php
FrontendBundle
Entity
Blog.php
Visit.php
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
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
Manual olarak symfony_backend
ve symfony_frontend
veritabanlarını yarattıktan sonra, aşağıdaki komutlar ile gerekli olan entitileri yaratabilirsiniz.
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!
Aşağıdaki argümentleri services.yml dosyasında kullanıp, asıl class içinde use Doctrine\ORM\EntityManager;
ile referans verebilirsiniz.
@doctrine.orm.backend_entity_manager
@doctrine.orm.frontend_entity_manager
# 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()
;
}
}
$this->kernel->getContainer()->get('doctrine.orm.backend_entity_manager');
$this->container->get('doctrine.orm.backend_entity_manager');