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 | |
parent | 54a2241e136ccf90c659b5699af4489b6e4d2da1 (diff) | |
download | wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.tar.gz wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.tar.zst wallabag-bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d.zip |
Quoted entity to avoid reserved keyword
Should fix #1498
-rw-r--r-- | app/config/config.yml | 1 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php | 83 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Entity/Config.php | 2 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Entity/Entry.php | 2 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Entity/Tag.php | 2 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Resources/config/services.yml | 6 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php | 47 | ||||
-rw-r--r-- | src/Wallabag/UserBundle/Entity/User.php | 2 |
8 files changed, 55 insertions, 90 deletions
diff --git a/app/config/config.yml b/app/config/config.yml index 0655fed5..285fbd7c 100644 --- a/app/config/config.yml +++ b/app/config/config.yml | |||
@@ -88,7 +88,6 @@ doctrine: | |||
88 | auto_generate_proxy_classes: "%kernel.debug%" | 88 | auto_generate_proxy_classes: "%kernel.debug%" |
89 | entity_managers: | 89 | entity_managers: |
90 | default: | 90 | default: |
91 | naming_strategy: wallabag_core.doctrine.prefixed_naming_strategy | ||
92 | auto_mapping: true | 91 | auto_mapping: true |
93 | 92 | ||
94 | # Swiftmailer Configuration | 93 | # Swiftmailer Configuration |
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 | |||
3 | namespace Wallabag\CoreBundle\Doctrine\Mapping; | ||
4 | |||
5 | use 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 | */ | ||
14 | class 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 | */ |
15 | class Config | 15 | class 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 | |||
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 | } | ||
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index d2efd200..e3b9a519 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php | |||
@@ -19,7 +19,7 @@ use Wallabag\CoreBundle\Entity\Tag; | |||
19 | * User. | 19 | * User. |
20 | * | 20 | * |
21 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") | 21 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") |
22 | * @ORM\Table | 22 | * @ORM\Table(name="`user`") |
23 | * @ORM\HasLifecycleCallbacks() | 23 | * @ORM\HasLifecycleCallbacks() |
24 | * @ExclusionPolicy("all") | 24 | * @ExclusionPolicy("all") |
25 | * | 25 | * |