Commit f4514d2c by Julien Jorry

Add TablePrefix => set env variable DATABASE_PREFIX if you want to use database prefix

parent d2baec74
...@@ -52,7 +52,7 @@ services: ...@@ -52,7 +52,7 @@ services:
# this creates a service per class whose id is the fully-qualified class name # this creates a service per class whose id is the fully-qualified class name
App\: App\:
resource: '../src/*' resource: '../src/*'
exclude: '../src/{DependencyInjection,Factory,Listener,Entity,Migrations,Tests,Twig,Kernel.php}' exclude: '../src/{DependencyInjection,Factory,Doctrine,Listener,Entity,Migrations,Tests,Twig,Kernel.php}'
# controllers are imported separately to make sure services can be injected # controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class # as action arguments even if you don't extend any base controller class
...@@ -63,6 +63,13 @@ services: ...@@ -63,6 +63,13 @@ services:
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones
# Définition du prefix pour les tables
App\Doctrine\TablePrefix:
arguments:
$prefix: '%env(string:DATABASE_PREFIX)%'
tags:
- { name: doctrine.event_subscriber, connection: default }
# cache doctrine for bazinga geocoder # cache doctrine for bazinga geocoder
mlc_cache_adapter: mlc_cache_adapter:
class: Doctrine\Common\Cache\ApcCache class: Doctrine\Common\Cache\ApcCache
......
<?php
namespace App\Doctrine;
use Doctrine\Common\EventSubscriber;
use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
class TablePrefix implements EventSubscriber
{
protected $prefix = '';
public function __construct($prefix)
{
$this->prefix = (string) $prefix;
}
public function getSubscribedEvents()
{
return ['loadClassMetadata'];
}
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();
if (!$classMetadata->isInheritanceTypeSingleTable() || $classMetadata->getName() === $classMetadata->rootEntityName) {
$classMetadata->setPrimaryTable([
'name' => $this->prefix . $classMetadata->getTableName()
]);
}
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide']) {
$mappedTableName = $mapping['joinTable']['name'];
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment