1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract class for all Models class |
9
|
|
|
* |
10
|
|
|
* @package bfw-sql |
11
|
|
|
* @author Vermeulen Maxime <[email protected]> |
12
|
|
|
* @version 2.0 |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractModels extends \BfwSql\Sql |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @const ERR_NO_CONNECTION_CONFIGURED Exception code if no connection |
18
|
|
|
* is configured. |
19
|
|
|
*/ |
20
|
|
|
const ERR_NO_CONNECTION_CONFIGURED = 2101001; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @const ERR_NEED_BASEKEYNAME_DEFINED Exception code if the baseKeyName |
24
|
|
|
* should be defined (multiple connection) but it's not. |
25
|
|
|
*/ |
26
|
|
|
const ERR_NEED_BASEKEYNAME_DEFINED = 2101002; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @const ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME Exception code if the |
30
|
|
|
* baseKeyName defined corresponding to no defined connection. |
31
|
|
|
*/ |
32
|
|
|
const ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME = 2101003; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string $tableName The table name |
36
|
|
|
*/ |
37
|
|
|
protected $tableName = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string $tableNameWithPrefix the table name with prefix |
41
|
|
|
*/ |
42
|
|
|
protected $tableNameWithPrefix = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string $alias The table alias to use into queries |
46
|
|
|
*/ |
47
|
|
|
protected $alias = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string $baseKeyName The baseKeyName to use to connection. |
51
|
|
|
* Use it if they are multiple database to connect in the application. |
52
|
|
|
*/ |
53
|
|
|
protected $baseKeyName = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor |
57
|
|
|
*/ |
58
|
|
|
public function __construct() |
59
|
|
|
{ |
60
|
|
|
$sqlConnect = $this->obtainSqlConnect(); |
61
|
|
|
|
62
|
|
|
parent::__construct($sqlConnect); |
63
|
|
|
$this->tableNameWithPrefix = $this->prefix.$this->tableName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Getter to property tableName |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getTableName(): string |
72
|
|
|
{ |
73
|
|
|
return $this->tableName; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Getter to property tableNameWithPrefix |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getTableNameWithPrefix(): string |
82
|
|
|
{ |
83
|
|
|
return $this->tableNameWithPrefix; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Getter to property alias |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getAlias(): string |
92
|
|
|
{ |
93
|
|
|
return $this->alias; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Getter to property baseKeyName |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getBaseKeyName(): string |
102
|
|
|
{ |
103
|
|
|
return $this->baseKeyName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the BFW Application |
108
|
|
|
* It's a dedicated method for unit test or case where App is override |
109
|
|
|
* |
110
|
|
|
* @return \BFW\Application |
111
|
|
|
*/ |
112
|
|
|
protected function obtainApp(): \BFW\Application |
113
|
|
|
{ |
114
|
|
|
return \BFW\Application::getInstance(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Obtain SqlConnect instance for the baseKeyName |
119
|
|
|
* |
120
|
|
|
* @return \BfwSql\SqlConnect |
121
|
|
|
* |
122
|
|
|
* @throws \Exception If there are many connection declared and if the |
123
|
|
|
* property baseKeyName is empty |
124
|
|
|
*/ |
125
|
|
|
protected function obtainSqlConnect(): \BfwSql\SqlConnect |
126
|
|
|
{ |
127
|
|
|
$listBases = $this->obtainApp() |
128
|
|
|
->getModuleList() |
129
|
|
|
->getModuleByName('bfw-sql') |
130
|
|
|
->listBases; |
131
|
|
|
|
132
|
|
|
if (count($listBases) === 0) { |
133
|
|
|
throw new Exception( |
134
|
|
|
'There is no connection configured.', |
135
|
|
|
self::ERR_NO_CONNECTION_CONFIGURED |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (count($listBases) > 1 && empty($this->baseKeyName)) { |
140
|
|
|
throw new Exception( |
141
|
|
|
'There are multiple connection, ' |
142
|
|
|
.'so the property baseKeyName must be defined', |
143
|
|
|
self::ERR_NEED_BASEKEYNAME_DEFINED |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (count($listBases) > 1 && !isset($listBases[$this->baseKeyName])) { |
148
|
|
|
throw new Exception( |
149
|
|
|
'There are multiple connection, ' |
150
|
|
|
.'but the connection '.$this->baseKeyName.' is not defined.', |
151
|
|
|
self::ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (count($listBases) === 1) { |
156
|
|
|
$sqlConnect = reset($listBases); |
157
|
|
|
} else { |
158
|
|
|
$sqlConnect = $listBases[$this->baseKeyName]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $sqlConnect; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function obtainTableInfos() |
165
|
|
|
{ |
166
|
|
|
if (empty($this->alias)) { |
167
|
|
|
return $this->tableName; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return [$this->alias => $this->tableName]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function select(string $type = 'array'): \BfwSql\Queries\Select |
174
|
|
|
{ |
175
|
|
|
return parent::select($type) |
176
|
|
|
->from($this->obtainTableInfos()) |
|
|
|
|
177
|
|
|
; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function insert( |
181
|
|
|
string $quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL |
182
|
|
|
): \BfwSql\Queries\Insert { |
183
|
|
|
return parent::insert($quoteStatus) |
184
|
|
|
->into($this->obtainTableInfos()) |
185
|
|
|
; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function update( |
189
|
|
|
string $quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL |
190
|
|
|
): \BfwSql\Queries\Update { |
191
|
|
|
return parent::update($quoteStatus) |
192
|
|
|
->from($this->obtainTableInfos()) |
193
|
|
|
; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function delete(): \BfwSql\Queries\Delete |
197
|
|
|
{ |
198
|
|
|
return parent::delete() |
199
|
|
|
->from($this->obtainTableInfos()) |
200
|
|
|
; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|