]>
Commit | Line | Data |
---|---|---|
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 | ||
76 | /** | |
77 | * {@inheritdoc} | |
78 | */ | |
79 | public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null) | |
80 | { | |
81 | return $propertyName.'_'.$embeddedColumnName; | |
82 | } | |
83 | } |