|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\EntityManager as EM; |
|
6
|
|
|
|
|
7
|
|
|
trait Naming |
|
8
|
|
|
{ |
|
9
|
|
|
/** The template to use to calculate the table name. |
|
10
|
|
|
* @var string */ |
|
11
|
|
|
protected static $tableNameTemplate; |
|
12
|
|
|
|
|
13
|
|
|
/** The naming scheme to use for table names. |
|
14
|
|
|
* @var string */ |
|
15
|
|
|
protected static $namingSchemeTable; |
|
16
|
|
|
|
|
17
|
|
|
/** The naming scheme to use for column names. |
|
18
|
|
|
* @var string */ |
|
19
|
|
|
protected static $namingSchemeColumn; |
|
20
|
|
|
|
|
21
|
|
|
/** The naming scheme to use for method names. |
|
22
|
|
|
* @var string */ |
|
23
|
|
|
protected static $namingSchemeMethods; |
|
24
|
|
|
|
|
25
|
|
|
/** Fixed table name (ignore other settings) |
|
26
|
|
|
* @var string */ |
|
27
|
|
|
protected static $tableName; |
|
28
|
|
|
|
|
29
|
|
|
/** The naming scheme to use for attributes. |
|
30
|
|
|
* @var string */ |
|
31
|
|
|
protected static $namingSchemeAttributes; |
|
32
|
|
|
|
|
33
|
|
|
/** Fixed column names (ignore other settings) |
|
34
|
|
|
* @var string[] */ |
|
35
|
|
|
protected static $columnAliases = []; |
|
36
|
|
|
|
|
37
|
|
|
/** A prefix for column names. |
|
38
|
|
|
* @var string */ |
|
39
|
|
|
protected static $columnPrefix; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the column name of $attribute |
|
43
|
|
|
* |
|
44
|
|
|
* The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
|
45
|
|
|
* to $namingSchemeColumn. |
|
46
|
|
|
* |
|
47
|
|
|
* **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
|
48
|
|
|
* the same as getColumnName($name). |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $attribute |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getColumnName($attribute) |
|
54
|
|
|
{ |
|
55
|
|
|
if (isset(static::$columnAliases[$attribute])) { |
|
56
|
|
|
return static::$columnAliases[$attribute]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return EM::getInstance(static::class)->getNamer() |
|
60
|
|
|
->getColumnName(static::class, $attribute, static::$columnPrefix, static::$namingSchemeColumn); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the column name of $attribute |
|
65
|
|
|
* |
|
66
|
|
|
* The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
|
67
|
|
|
* to $namingSchemeColumn. |
|
68
|
|
|
* |
|
69
|
|
|
* **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
|
70
|
|
|
* the same as getColumnName($name). |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $column |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getAttributeName($column) |
|
76
|
|
|
{ |
|
77
|
|
|
$attributeName = array_search($column, static::$columnAliases); |
|
78
|
|
|
if ($attributeName !== false) { |
|
79
|
|
|
return $attributeName; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return EM::getInstance(static::class)->getNamer() |
|
83
|
|
|
->getAttributeName($column, static::$columnPrefix, static::$namingSchemeAttributes); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the table name |
|
88
|
|
|
* |
|
89
|
|
|
* The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
|
90
|
|
|
* $tableName. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getTableName() |
|
95
|
|
|
{ |
|
96
|
|
|
if (static::$tableName) { |
|
97
|
|
|
return static::$tableName; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return EM::getInstance(static::class)->getNamer() |
|
101
|
|
|
->getTableName(static::class, static::$tableNameTemplate, static::$namingSchemeTable); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// DEPRECATED stuff |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
* @deprecated use getOption from EntityManager |
|
109
|
|
|
* @codeCoverageIgnore deprecated |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function getTableNameTemplate() |
|
112
|
|
|
{ |
|
113
|
|
|
return static::$tableNameTemplate; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $tableNameTemplate |
|
118
|
|
|
* @deprecated use setOption from EntityManager |
|
119
|
|
|
* @codeCoverageIgnore deprecated |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function setTableNameTemplate($tableNameTemplate) |
|
122
|
|
|
{ |
|
123
|
|
|
static::$tableNameTemplate = $tableNameTemplate; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
* @deprecated use getOption from EntityManager |
|
129
|
|
|
* @codeCoverageIgnore deprecated |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function getNamingSchemeTable() |
|
132
|
|
|
{ |
|
133
|
|
|
return static::$namingSchemeTable; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $namingSchemeTable |
|
138
|
|
|
* @deprecated use setOption from EntityManager |
|
139
|
|
|
* @codeCoverageIgnore deprecated |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function setNamingSchemeTable($namingSchemeTable) |
|
142
|
|
|
{ |
|
143
|
|
|
static::$namingSchemeTable = $namingSchemeTable; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
* @deprecated use getOption from EntityManager |
|
149
|
|
|
* @codeCoverageIgnore deprecated |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function getNamingSchemeColumn() |
|
152
|
|
|
{ |
|
153
|
|
|
return static::$namingSchemeColumn; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $namingSchemeColumn |
|
158
|
|
|
* @deprecated use setOption from EntityManager |
|
159
|
|
|
* @codeCoverageIgnore deprecated |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function setNamingSchemeColumn($namingSchemeColumn) |
|
162
|
|
|
{ |
|
163
|
|
|
static::$namingSchemeColumn = $namingSchemeColumn; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
* @deprecated use getOption from EntityManager |
|
169
|
|
|
* @codeCoverageIgnore deprecated |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function getNamingSchemeMethods() |
|
172
|
|
|
{ |
|
173
|
|
|
return static::$namingSchemeMethods; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $namingSchemeMethods |
|
178
|
|
|
* @deprecated use setOption from EntityManager |
|
179
|
|
|
* @codeCoverageIgnore deprecated |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function setNamingSchemeMethods($namingSchemeMethods) |
|
182
|
|
|
{ |
|
183
|
|
|
static::$namingSchemeMethods = $namingSchemeMethods; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|