]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Quoted entity to avoid reserved keyword
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 24 Oct 2015 13:28:02 +0000 (15:28 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 7 Nov 2015 13:15:33 +0000 (14:15 +0100)
Should fix #1498

app/config/config.yml
src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php [deleted file]
src/Wallabag/CoreBundle/Entity/Config.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php [new file with mode: 0644]
src/Wallabag/UserBundle/Entity/User.php

index 0655fed58a39c951fb61cf8dd7612957fee20174..285fbd7cf10f96a06d5b622923ebfe6b4c7bd499 100644 (file)
@@ -88,7 +88,6 @@ doctrine:
         auto_generate_proxy_classes: "%kernel.debug%"
         entity_managers:
             default:
-                naming_strategy: wallabag_core.doctrine.prefixed_naming_strategy
                 auto_mapping: true
 
 # Swiftmailer Configuration
diff --git a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
deleted file mode 100644 (file)
index 509348d..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-namespace Wallabag\CoreBundle\Doctrine\Mapping;
-
-use Doctrine\ORM\Mapping\NamingStrategy;
-
-/**
- * Puts a prefix to each table.
- *
- * Solution from :
- *      - http://stackoverflow.com/a/23860613/569101
- *      - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
- */
-class PrefixedNamingStrategy implements NamingStrategy
-{
-    protected $prefix = '';
-
-    public function __construct($prefix)
-    {
-        $this->prefix = (string) $prefix;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function classToTableName($className)
-    {
-        return strtolower($this->prefix.substr($className, strrpos($className, '\\') + 1));
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function propertyToColumnName($propertyName, $className = null)
-    {
-        return $propertyName;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function referenceColumnName()
-    {
-        return 'id';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function joinColumnName($propertyName)
-    {
-        return $propertyName.'_'.$this->referenceColumnName();
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
-    {
-        // for join table we don't want to have both table concatenated AND prefixed
-        // we just want the whole table to prefixed once
-        // ie: not "wallabag_entry_wallabag_tag" but "wallabag_entry_tag"
-        $target = substr($targetEntity, strrpos($targetEntity, '\\') + 1);
-
-        return strtolower($this->classToTableName($sourceEntity).'_'.$target);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function joinKeyColumnName($entityName, $referencedColumnName = null)
-    {
-        return strtolower($this->classToTableName($entityName).'_'.($referencedColumnName ?: $this->referenceColumnName()));
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
-    {
-        return $propertyName.'_'.$embeddedColumnName;
-    }
-}
index f4edcfa9f7af04cbbe2cd3ceda4b4aa0bd798660..b2a1915a4fdbd22f921e5ecae5212da9ff922d46 100644 (file)
@@ -9,7 +9,7 @@ use Symfony\Component\Validator\Constraints as Assert;
  * Config.
  *
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository")
- * @ORM\Table
+ * @ORM\Table(name="`config`")
  * @ORM\Entity
  */
 class Config
index 4fd74001b3efc076388f183fe794fea6180fba04..9e5446a64835db4e3f8fb920636cd0405e697d62 100644 (file)
@@ -14,7 +14,7 @@ use Wallabag\UserBundle\Entity\User;
  *
  * @XmlRoot("entry")
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
- * @ORM\Table
+ * @ORM\Table(name="`entry`")
  * @ORM\HasLifecycleCallbacks()
  * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
  */
index 5b571823f707f11659f7da8c92707ff839d60917..7cc452fd7f97b860f4eabadeaf9523ec95e4e5bd 100644 (file)
@@ -12,7 +12,7 @@ use Doctrine\Common\Collections\ArrayCollection;
  * Tag.
  *
  * @XmlRoot("tag")
- * @ORM\Table
+ * @ORM\Table(name="`tag`")
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
  * @ExclusionPolicy("all")
  */
index debbf39e0eb4ab9672e5ccad38567afd18062018..65c2c8d85dffdab025ca5c46d0d136da5398a5f5 100644 (file)
@@ -33,10 +33,12 @@ services:
         arguments:
             - @doctrine
 
-    wallabag_core.doctrine.prefixed_naming_strategy:
-        class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy
+    wallabag_core.table_prefix_subscriber:
+        class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber
         arguments:
             - %database_table_prefix%
+        tags:
+            - { name: doctrine.event_subscriber }
 
     wallabag_core.graby:
         class: Graby\Graby
diff --git a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
new file mode 100644 (file)
index 0000000..8ec85e6
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace Wallabag\CoreBundle\Subscriber;
+
+use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+use Doctrine\Common\EventSubscriber;
+use Doctrine\ORM\Mapping\ClassMetadataInfo;
+
+/**
+ * Puts a prefix to each table.
+ *
+ * Solution from :
+ *      - http://stackoverflow.com/a/23860613/569101
+ *      - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
+ */
+class TablePrefixSubscriber implements EventSubscriber
+{
+    protected $prefix = '';
+
+    public function __construct($prefix)
+    {
+        $this->prefix = (string) $prefix;
+    }
+
+    public function getSubscribedEvents()
+    {
+        return array('loadClassMetadata');
+    }
+
+    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
+    {
+        $classMetadata = $args->getClassMetadata();
+        // if we are in an inheritance hierarchy, only apply this once
+        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
+            return;
+        }
+
+        $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+
+        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+            if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
+                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
+                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
+            }
+        }
+    }
+}
index d2efd20080b4bee41b3d7b6badbbe94a7770c675..e3b9a519c7e42617ec816f98edefdf53a01cb2ad 100644 (file)
@@ -19,7 +19,7 @@ use Wallabag\CoreBundle\Entity\Tag;
  * User.
  *
  * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
- * @ORM\Table
+ * @ORM\Table(name="`user`")
  * @ORM\HasLifecycleCallbacks()
  * @ExclusionPolicy("all")
  *