aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Subscriber
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-10-24 15:28:02 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-11-07 14:15:33 +0100
commitbd0f3d32c9ccb8f7a1409edb960b909a5e6a096d (patch)
tree80b891432be989e7dbfa2817f0f965e67690ebe5 /src/Wallabag/CoreBundle/Subscriber
parent54a2241e136ccf90c659b5699af4489b6e4d2da1 (diff)
downloadwallabag-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.php47
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
3namespace Wallabag\CoreBundle\Subscriber;
4
5use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6use Doctrine\Common\EventSubscriber;
7use 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 */
16class 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}