aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-11-09 16:31:59 +0100
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2015-11-09 16:31:59 +0100
commitf1eccfd63f214dcc730ab0d18a694a5465f425db (patch)
treee6cbd355f3a360e5619821fb1415ee26a01f11ce /src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
parent4529d0f4b6b20cbbd1ccb5339a753aff7d35552b (diff)
parent53cf5106891fc64f3f66cb6b3316654a9620239c (diff)
downloadwallabag-f1eccfd63f214dcc730ab0d18a694a5465f425db.tar.gz
wallabag-f1eccfd63f214dcc730ab0d18a694a5465f425db.tar.zst
wallabag-f1eccfd63f214dcc730ab0d18a694a5465f425db.zip
Merge pull request #1500 from wallabag/v2-quote-pgsql
Fix quote strategy for reserved keyword in Postgres
Diffstat (limited to 'src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php')
-rw-r--r--src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php51
1 files changed, 51 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..0019eead
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
@@ -0,0 +1,51 @@
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 * 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.
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 {
30 return array('loadClassMetadata');
31 }
32
33 public function loadClassMetadata(LoadClassMetadataEventArgs $args)
34 {
35 $classMetadata = $args->getClassMetadata();
36
37 // if we are in an inheritance hierarchy, only apply this once
38 if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
39 return;
40 }
41
42 $classMetadata->setTableName($this->prefix.$classMetadata->getTableName());
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'];
47 $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName;
48 }
49 }
50 }
51}