diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-11-03 16:22:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-03 16:22:07 +0100 |
commit | da4136557963018287cae61226e9006c3c741747 (patch) | |
tree | 6bf23c0d32f3780843798ae47c65377ae1ff961a /src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php | |
parent | 816c0940d142f8ec69bdd3f302c9751033cce7e2 (diff) | |
parent | ca08d02f2f17c0e4ec0152a701c540f62713a428 (diff) | |
download | wallabag-da4136557963018287cae61226e9006c3c741747.tar.gz wallabag-da4136557963018287cae61226e9006c3c741747.tar.zst wallabag-da4136557963018287cae61226e9006c3c741747.zip |
Merge pull request #2180 from wallabag/download-pictures
Download pictures
Diffstat (limited to 'src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php new file mode 100644 index 00000000..9013328f --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php | |||
@@ -0,0 +1,51 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Event\Subscriber; | ||
4 | |||
5 | use Doctrine\Common\EventSubscriber; | ||
6 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
7 | use 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 | */ | ||
19 | class 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 ['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 | } | ||