]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
Merge pull request #2239 from wallabag/documentation-2.1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Subscriber / TablePrefixSubscriber.php
CommitLineData
bd0f3d32
JB
1<?php
2
3namespace Wallabag\CoreBundle\Subscriber;
4
bd0f3d32 5use Doctrine\Common\EventSubscriber;
619cc453 6use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
bd0f3d32
JB
7use Doctrine\ORM\Mapping\ClassMetadataInfo;
8
9/**
10 * Puts a prefix to each table.
735068d1
JB
11 * This way were used instead of using the built-in strategy from Doctrine, using `naming_strategy`
12 * Because it conflicts with the DefaultQuoteStrategy (that espace table name, like user for Postgres)
13 * see #1498 for more detail.
bd0f3d32
JB
14 *
15 * Solution from :
16 * - http://stackoverflow.com/a/23860613/569101
17 * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
18 */
19class TablePrefixSubscriber implements EventSubscriber
20{
21 protected $prefix = '';
22
23 public function __construct($prefix)
24 {
25 $this->prefix = (string) $prefix;
26 }
27
28 public function getSubscribedEvents()
29 {
4094ea47 30 return ['loadClassMetadata'];
bd0f3d32
JB
31 }
32
33 public function loadClassMetadata(LoadClassMetadataEventArgs $args)
34 {
35 $classMetadata = $args->getClassMetadata();
735068d1 36
bd0f3d32
JB
37 // if we are in an inheritance hierarchy, only apply this once
38 if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
39 return;
40 }
41
735068d1 42 $classMetadata->setTableName($this->prefix.$classMetadata->getTableName());
bd0f3d32
JB
43
44 foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
45 if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
46 $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
735068d1 47 $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName;
bd0f3d32
JB
48 }
49 }
50 }
51}