diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-10-24 15:28:02 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-11-07 14:15:33 +0100 |
commit | bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d (patch) | |
tree | 80b891432be989e7dbfa2817f0f965e67690ebe5 /src/Wallabag/CoreBundle/Subscriber | |
parent | 54a2241e136ccf90c659b5699af4489b6e4d2da1 (diff) | |
download | wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.tar.gz wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.tar.zst wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.zip |
Quoted entity to avoid reserved keyword
Should fix #1498
Diffstat (limited to 'src/Wallabag/CoreBundle/Subscriber')
-rw-r--r-- | src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php new file mode 100644 index 00000000..8ec85e64 --- /dev/null +++ b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php | |||
@@ -0,0 +1,47 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Subscriber; | ||
4 | |||
5 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
6 | use Doctrine\Common\EventSubscriber; | ||
7 | use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
8 | |||
9 | /** | ||
10 | * Puts a prefix to each table. | ||
11 | * | ||
12 | * Solution from : | ||
13 | * - http://stackoverflow.com/a/23860613/569101 | ||
14 | * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html | ||
15 | */ | ||
16 | class TablePrefixSubscriber implements EventSubscriber | ||
17 | { | ||
18 | protected $prefix = ''; | ||
19 | |||
20 | public function __construct($prefix) | ||
21 | { | ||
22 | $this->prefix = (string) $prefix; | ||
23 | } | ||
24 | |||
25 | public function getSubscribedEvents() | ||
26 | { | ||
27 | return array('loadClassMetadata'); | ||
28 | } | ||
29 | |||
30 | public function loadClassMetadata(LoadClassMetadataEventArgs $args) | ||
31 | { | ||
32 | $classMetadata = $args->getClassMetadata(); | ||
33 | // if we are in an inheritance hierarchy, only apply this once | ||
34 | if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { | ||
35 | return; | ||
36 | } | ||
37 | |||
38 | $classMetadata->setTableName($this->prefix . $classMetadata->getTableName()); | ||
39 | |||
40 | foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { | ||
41 | if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { | ||
42 | $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; | ||
43 | $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName; | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | } | ||