aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php83
-rw-r--r--src/Wallabag/CoreBundle/Entity/Config.php2
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php2
-rw-r--r--src/Wallabag/CoreBundle/Entity/Tag.php2
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml6
-rw-r--r--src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php47
6 files changed, 54 insertions, 88 deletions
diff --git a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
deleted file mode 100644
index 509348db..00000000
--- a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
+++ /dev/null
@@ -1,83 +0,0 @@
1<?php
2
3namespace Wallabag\CoreBundle\Doctrine\Mapping;
4
5use Doctrine\ORM\Mapping\NamingStrategy;
6
7/**
8 * Puts a prefix to each table.
9 *
10 * Solution from :
11 * - http://stackoverflow.com/a/23860613/569101
12 * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
13 */
14class PrefixedNamingStrategy implements NamingStrategy
15{
16 protected $prefix = '';
17
18 public function __construct($prefix)
19 {
20 $this->prefix = (string) $prefix;
21 }
22
23 /**
24 * {@inheritdoc}
25 */
26 public function classToTableName($className)
27 {
28 return strtolower($this->prefix.substr($className, strrpos($className, '\\') + 1));
29 }
30
31 /**
32 * {@inheritdoc}
33 */
34 public function propertyToColumnName($propertyName, $className = null)
35 {
36 return $propertyName;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function referenceColumnName()
43 {
44 return 'id';
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function joinColumnName($propertyName)
51 {
52 return $propertyName.'_'.$this->referenceColumnName();
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
59 {
60 // for join table we don't want to have both table concatenated AND prefixed
61 // we just want the whole table to prefixed once
62 // ie: not "wallabag_entry_wallabag_tag" but "wallabag_entry_tag"
63 $target = substr($targetEntity, strrpos($targetEntity, '\\') + 1);
64
65 return strtolower($this->classToTableName($sourceEntity).'_'.$target);
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function joinKeyColumnName($entityName, $referencedColumnName = null)
72 {
73 return strtolower($this->classToTableName($entityName).'_'.($referencedColumnName ?: $this->referenceColumnName()));
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
80 {
81 return $propertyName.'_'.$embeddedColumnName;
82 }
83}
diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php
index f4edcfa9..b2a1915a 100644
--- a/src/Wallabag/CoreBundle/Entity/Config.php
+++ b/src/Wallabag/CoreBundle/Entity/Config.php
@@ -9,7 +9,7 @@ use Symfony\Component\Validator\Constraints as Assert;
9 * Config. 9 * Config.
10 * 10 *
11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository") 11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository")
12 * @ORM\Table 12 * @ORM\Table(name="`config`")
13 * @ORM\Entity 13 * @ORM\Entity
14 */ 14 */
15class Config 15class Config
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index 4fd74001..9e5446a6 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -14,7 +14,7 @@ use Wallabag\UserBundle\Entity\User;
14 * 14 *
15 * @XmlRoot("entry") 15 * @XmlRoot("entry")
16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") 16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
17 * @ORM\Table 17 * @ORM\Table(name="`entry`")
18 * @ORM\HasLifecycleCallbacks() 18 * @ORM\HasLifecycleCallbacks()
19 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") 19 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
20 */ 20 */
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php
index 5b571823..7cc452fd 100644
--- a/src/Wallabag/CoreBundle/Entity/Tag.php
+++ b/src/Wallabag/CoreBundle/Entity/Tag.php
@@ -12,7 +12,7 @@ use Doctrine\Common\Collections\ArrayCollection;
12 * Tag. 12 * Tag.
13 * 13 *
14 * @XmlRoot("tag") 14 * @XmlRoot("tag")
15 * @ORM\Table 15 * @ORM\Table(name="`tag`")
16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") 16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
17 * @ExclusionPolicy("all") 17 * @ExclusionPolicy("all")
18 */ 18 */
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index debbf39e..65c2c8d8 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -33,10 +33,12 @@ services:
33 arguments: 33 arguments:
34 - @doctrine 34 - @doctrine
35 35
36 wallabag_core.doctrine.prefixed_naming_strategy: 36 wallabag_core.table_prefix_subscriber:
37 class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy 37 class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber
38 arguments: 38 arguments:
39 - %database_table_prefix% 39 - %database_table_prefix%
40 tags:
41 - { name: doctrine.event_subscriber }
40 42
41 wallabag_core.graby: 43 wallabag_core.graby:
42 class: Graby\Graby 44 class: Graby\Graby
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}