mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-09 00:15:27 +00:00
parent
54a2241e13
commit
bd0f3d32c9
8 changed files with 55 additions and 90 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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())")
|
||||
*/
|
||||
|
|
|
@ -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")
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
47
src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
Normal file
47
src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue