From 164bd801188245942ca996eda75d7a554f29746a Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sat, 28 Mar 2015 11:29:19 +0100 Subject: [PATCH] Ability to prefix tables Will fix #799 --- app/config/config.yml | 5 +- app/config/parameters.yml.dist | 1 + .../CoreBundle/Command/InstallCommand.php | 5 +- .../Mapping/PrefixedNamingStrategy.php | 75 +++++++++++++++++++ src/Wallabag/CoreBundle/Entity/Config.php | 2 +- src/Wallabag/CoreBundle/Entity/Entry.php | 4 +- src/Wallabag/CoreBundle/Entity/Tag.php | 2 +- src/Wallabag/CoreBundle/Entity/User.php | 2 +- .../CoreBundle/Resources/config/services.yml | 4 + 9 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php diff --git a/app/config/config.yml b/app/config/config.yml index 367aa276..0807b06a 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -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: diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index 73318013..f80f65ad 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -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 diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index bba2607d..493842f7 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -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 index 00000000..861a60ea --- /dev/null +++ b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php @@ -0,0 +1,75 @@ +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())); + } +} diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index 9f079656..62ea637e 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; * Config * * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository") - * @ORM\Table(name="config") + * @ORM\Table * @ORM\Entity */ class Config diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 75aeae84..15af105d 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -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; diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 9ae5867c..9d3c7a32 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -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") */ diff --git a/src/Wallabag/CoreBundle/Entity/User.php b/src/Wallabag/CoreBundle/Entity/User.php index e75e3a83..1652170f 100644 --- a/src/Wallabag/CoreBundle/Entity/User.php +++ b/src/Wallabag/CoreBundle/Entity/User.php @@ -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") */ diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 0f4db94e..cea6c0df 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -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%] -- 2.41.0