Quoted entity to avoid reserved keyword

Should fix #1498
This commit is contained in:
Jeremy Benoist 2015-10-24 15:28:02 +02:00
parent 54a2241e13
commit bd0f3d32c9
8 changed files with 55 additions and 90 deletions

View 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

View file

@ -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;
}
}

View 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

View 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())")
*/

View 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")
*/

View 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

View file

@ -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;
}
}
}
}

View 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")
*