diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-04-01 21:53:48 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-04-01 21:53:48 +0200 |
commit | d6fa2f70ac3c798b28ce33c6c143f45c09c74062 (patch) | |
tree | 40769c29279d189dfe0dbb8aa513122f9172ebde /src/Wallabag/CoreBundle/Doctrine | |
parent | 1a93ee423b072ec3bcb0c437cbf9b488bdea245c (diff) | |
parent | 276a1e9d3fb7e83569d16bcb37a67607d4ace8be (diff) | |
download | wallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.tar.gz wallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.tar.zst wallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.zip |
Merge pull request #1165 from wallabag/v2-prefix-db
Prefix table
Diffstat (limited to 'src/Wallabag/CoreBundle/Doctrine')
-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 | } | ||