]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Ability to prefix tables
authorJeremy <jeremy.benoist@gmail.com>
Sat, 28 Mar 2015 10:29:19 +0000 (11:29 +0100)
committerNicolas LÅ“uillet <nicolas@loeuillet.org>
Wed, 1 Apr 2015 19:32:02 +0000 (21:32 +0200)
Will fix #799

app/config/config.yml
app/config/parameters.yml.dist
src/Wallabag/CoreBundle/Command/InstallCommand.php
src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Config.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Entity/User.php
src/Wallabag/CoreBundle/Resources/config/services.yml

index 367aa2763537dff1868576d823b42cbc014c06d3..0807b06ab024ef56c89773e753af55e44f1352a7 100644 (file)
@@ -75,7 +75,10 @@ doctrine:
 
     orm:
         auto_generate_proxy_classes: "%kernel.debug%"
-        auto_mapping: true
+        entity_managers:
+            default:
+                naming_strategy: wallabag_core.doctrine.prefixed_naming_strategy
+                auto_mapping: true
 
 # Swiftmailer Configuration
 swiftmailer:
index 7331801336fe005c139ff4c6544dab56726b9ecf..f80f65ad87136fbd85e96084302c6ac9bc52623b 100644 (file)
@@ -7,6 +7,7 @@ parameters:
     database_user: root
     database_password: ~
     database_path: "%kernel.root_dir%/../data/db/wallabag.sqlite"
+    database_table_prefix: wallabag_
 
     mailer_transport:  smtp
     mailer_host:       127.0.0.1
index bba2607d05ae6c5ffd80585dbe268d2d97ffd98f..493842f7959962818fd5127207afa77982a947c3 100644 (file)
@@ -299,7 +299,8 @@ class InstallCommand extends ContainerAwareCommand
     }
 
     /**
-     * Check if the schema is already created
+     * Check if the schema is already created.
+     * If we found at least oen table, it means the schema exists
      *
      * @return boolean
      */
@@ -307,6 +308,6 @@ class InstallCommand extends ContainerAwareCommand
     {
         $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
 
-        return $schemaManager->tablesExist(array('entry'));
+        return count($schemaManager->listTableNames()) > 0 ? true : false;
     }
 }
diff --git a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
new file mode 100644 (file)
index 0000000..861a60e
--- /dev/null
@@ -0,0 +1,75 @@
+<?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()));
+    }
+}
index 9f079656ed7aa66ec7a1b9e5bd54e8495d53a510..62ea637eaaa4e64debd5b99262afb92b7bee477b 100644 (file)
@@ -9,7 +9,7 @@ use Symfony\Component\Validator\Constraints as Assert;
  * Config
  *
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository")
- * @ORM\Table(name="config")
+ * @ORM\Table
  * @ORM\Entity
  */
 class Config
index 75aeae84bffff83d0124f2c3d06172e82d047240..15af105d22f78c7c9f25156a9332108d1e0aef70 100644 (file)
@@ -13,7 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot;
  *
  * @XmlRoot("entry")
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
- * @ORM\Table(name="entry")
+ * @ORM\Table
  * @ORM\HasLifecycleCallbacks()
  * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
  */
@@ -121,7 +121,7 @@ class Entry
 
     /**
      * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
-     * @ORM\JoinTable(name="entry_tags")
+     * @ORM\JoinTable
      */
     private $tags;
 
index 9ae5867c6981ceccb04fbff2c24e1ef2d5a1567e..9d3c7a32105327cceccadecc0f74711d6d09c3ff 100644 (file)
@@ -12,7 +12,7 @@ use Doctrine\Common\Collections\ArrayCollection;
  * Tag
  *
  * @XmlRoot("tag")
- * @ORM\Table(name="tag")
+ * @ORM\Table
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
  * @ExclusionPolicy("all")
  */
index e75e3a8377bb21615ec7e93279f0c54ab306240e..1652170f7a7c8eefd2c1bcdd0674be4aedd29759 100644 (file)
@@ -13,8 +13,8 @@ use JMS\Serializer\Annotation\Expose;
 /**
  * User
  *
- * @ORM\Table(name="user")
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository")
+ * @ORM\Table
  * @ORM\HasLifecycleCallbacks()
  * @ExclusionPolicy("all")
  */
index 0f4db94e7321791bdcd4f0f679bd040e5c19d43e..cea6c0df1098681478686cf8a883e21e799dc79e 100644 (file)
@@ -43,3 +43,7 @@ services:
             - { name: request.param_converter, converter: username_rsstoken_converter }
         arguments:
             - @doctrine
+
+    wallabag_core.doctrine.prefixed_naming_strategy:
+        class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy
+        arguments: [%database_table_prefix%]