Ability to prefix tables

Will fix #799
This commit is contained in:
Jeremy 2015-03-28 11:29:19 +01:00 committed by Nicolas Lœuillet
parent 1a93ee423b
commit 164bd80118
9 changed files with 92 additions and 8 deletions

View 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:

View 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

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

View file

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

View 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

View 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;

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

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

View 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%]