diff options
author | Jeremy <jeremy.benoist@gmail.com> | 2015-03-28 11:29:19 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-04-01 21:32:02 +0200 |
commit | 164bd801188245942ca996eda75d7a554f29746a (patch) | |
tree | 6e4892446a97f9a2b38a781ca09154da93a82646 /src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php | |
parent | 1a93ee423b072ec3bcb0c437cbf9b488bdea245c (diff) | |
download | wallabag-164bd801188245942ca996eda75d7a554f29746a.tar.gz wallabag-164bd801188245942ca996eda75d7a554f29746a.tar.zst wallabag-164bd801188245942ca996eda75d7a554f29746a.zip |
Ability to prefix tables
Will fix #799
Diffstat (limited to 'src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php | 75 |
1 files changed, 75 insertions, 0 deletions
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Doctrine\Mapping; | ||
4 | |||
5 | use Doctrine\ORM\Mapping\NamingStrategy; | ||
6 | |||
7 | /** | ||
8 | * Puts a prefix to each table. | ||
9 | * | ||
10 | * Solution from : | ||
11 | * - http://stackoverflow.com/a/23860613/569101 | ||
12 | * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html | ||
13 | */ | ||
14 | class PrefixedNamingStrategy implements NamingStrategy | ||
15 | { | ||
16 | protected $prefix = ''; | ||
17 | |||
18 | public function __construct($prefix) | ||
19 | { | ||
20 | $this->prefix = (string) $prefix; | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * {@inheritdoc} | ||
25 | */ | ||
26 | public function classToTableName($className) | ||
27 | { | ||
28 | return strtolower($this->prefix . substr($className, strrpos($className, '\\') + 1)); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * {@inheritdoc} | ||
33 | */ | ||
34 | public function propertyToColumnName($propertyName, $className = null) | ||
35 | { | ||
36 | return $propertyName; | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * {@inheritdoc} | ||
41 | */ | ||
42 | public function referenceColumnName() | ||
43 | { | ||
44 | return 'id'; | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * {@inheritdoc} | ||
49 | */ | ||
50 | public function joinColumnName($propertyName) | ||
51 | { | ||
52 | return $propertyName . '_' . $this->referenceColumnName(); | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * {@inheritdoc} | ||
57 | */ | ||
58 | public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) | ||
59 | { | ||
60 | // for join table we don't want to have both table concatenated AND prefixed | ||
61 | // we just want the whole table to prefixed once | ||
62 | // ie: not "wallabag_entry_wallabag_tag" but "wallabag_entry_tag" | ||
63 | $target = substr($targetEntity, strrpos($targetEntity, '\\') + 1); | ||
64 | |||
65 | return strtolower($this->classToTableName($sourceEntity) . '_' .$target); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * {@inheritdoc} | ||
70 | */ | ||
71 | public function joinKeyColumnName($entityName, $referencedColumnName = null) | ||
72 | { | ||
73 | return strtolower($this->classToTableName($entityName) . '_' .($referencedColumnName ?: $this->referenceColumnName())); | ||
74 | } | ||
75 | } | ||