From bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 24 Oct 2015 15:28:02 +0200 Subject: [PATCH] Quoted entity to avoid reserved keyword Should fix #1498 --- app/config/config.yml | 1 - .../Mapping/PrefixedNamingStrategy.php | 83 ------------------- src/Wallabag/CoreBundle/Entity/Config.php | 2 +- src/Wallabag/CoreBundle/Entity/Entry.php | 2 +- src/Wallabag/CoreBundle/Entity/Tag.php | 2 +- .../CoreBundle/Resources/config/services.yml | 6 +- .../Subscriber/TablePrefixSubscriber.php | 47 +++++++++++ src/Wallabag/UserBundle/Entity/User.php | 2 +- 8 files changed, 55 insertions(+), 90 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php create mode 100644 src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php 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: 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 index 509348db..00000000 --- a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php +++ /dev/null @@ -1,83 +0,0 @@ -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; - } -} 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; * Config. * * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository") - * @ORM\Table + * @ORM\Table(name="`config`") * @ORM\Entity */ 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; * * @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())") */ 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; * Tag. * * @XmlRoot("tag") - * @ORM\Table + * @ORM\Table(name="`tag`") * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") * @ExclusionPolicy("all") */ 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: 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 index 00000000..8ec85e64 --- /dev/null +++ b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php @@ -0,0 +1,47 @@ +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; + } + } + } +} 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; * User. * * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") - * @ORM\Table + * @ORM\Table(name="`user`") * @ORM\HasLifecycleCallbacks() * @ExclusionPolicy("all") * -- 2.41.0