1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm; |
4
|
|
|
|
5
|
|
|
use Ubiquity\db\Database; |
6
|
|
|
use Ubiquity\log\Logger; |
7
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
8
|
|
|
use Ubiquity\db\SqlUtils; |
9
|
|
|
use Ubiquity\orm\parser\Reflexion; |
10
|
|
|
use Ubiquity\orm\traits\DAOUpdatesTrait; |
11
|
|
|
use Ubiquity\orm\traits\DAORelationsTrait; |
12
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
13
|
|
|
use Ubiquity\orm\traits\DAOUQueries; |
14
|
|
|
use Ubiquity\orm\traits\DAOCoreTrait; |
15
|
|
|
use Ubiquity\orm\traits\DAORelationsPrepareTrait; |
16
|
|
|
use Ubiquity\exceptions\DAOException; |
17
|
|
|
use Ubiquity\orm\traits\DAORelationsAssignmentsTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Gateway class between database and object model |
21
|
|
|
* This class is part of Ubiquity |
22
|
|
|
* |
23
|
|
|
* @author jcheron <[email protected]> |
24
|
|
|
* @version 1.1.5 |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
class DAO { |
28
|
|
|
use DAOCoreTrait,DAOUpdatesTrait,DAORelationsTrait,DAORelationsPrepareTrait,DAORelationsAssignmentsTrait,DAOUQueries; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var Database |
33
|
|
|
*/ |
34
|
|
|
public static $db; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Loads member associated with $instance by a ManyToOne relationship |
38
|
|
|
* |
39
|
|
|
* @param object $instance |
40
|
|
|
* @param string $member |
41
|
|
|
* @param boolean|array $included |
42
|
|
|
* if true, loads associate members with associations, if array, example : ["client.*","commands"] |
43
|
|
|
* @param boolean|null $useCache |
44
|
|
|
*/ |
45
|
1 |
|
public static function getManyToOne($instance, $member, $included = false, $useCache = NULL) { |
46
|
1 |
|
$fieldAnnot = OrmUtils::getMemberJoinColumns ( $instance, $member ); |
47
|
1 |
|
if ($fieldAnnot !== null) { |
48
|
1 |
|
$annotationArray = $fieldAnnot [1]; |
49
|
1 |
|
$member = $annotationArray ["member"]; |
50
|
1 |
|
$value = Reflexion::getMemberValue ( $instance, $member ); |
51
|
1 |
|
$key = OrmUtils::getFirstKey ( $annotationArray ["className"] ); |
52
|
1 |
|
$kv = array ($key => $value ); |
53
|
1 |
|
$obj = self::getOne ( $annotationArray ["className"], $kv, $included, null, $useCache ); |
54
|
1 |
|
if ($obj !== null) { |
55
|
1 |
|
Logger::info ( "DAO", "Loading the member " . $member . " for the object " . \get_class ( $instance ), "getManyToOne" ); |
56
|
1 |
|
$accesseur = "set" . ucfirst ( $member ); |
57
|
1 |
|
if (method_exists ( $instance, $accesseur )) { |
58
|
1 |
|
$instance->$accesseur ( $obj ); |
59
|
1 |
|
$instance->_rest [$member] = $obj->_rest; |
60
|
1 |
|
return $obj; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Assign / load the child records in the $member member of $instance. |
68
|
|
|
* |
69
|
|
|
* @param object $instance |
70
|
|
|
* @param string $member |
71
|
|
|
* Member on which a oneToMany annotation must be present |
72
|
|
|
* @param boolean|array $included |
73
|
|
|
* if true, loads associate members with associations, if array, example : ["client.*","commands"] |
74
|
|
|
* @param boolean $useCache |
75
|
|
|
* @param array $annot |
76
|
|
|
* used internally |
77
|
|
|
*/ |
78
|
1 |
|
public static function getOneToMany($instance, $member, $included = true, $useCache = NULL, $annot = null) { |
79
|
1 |
|
$ret = array (); |
80
|
1 |
|
$class = get_class ( $instance ); |
81
|
1 |
|
if (! isset ( $annot )) |
82
|
1 |
|
$annot = OrmUtils::getAnnotationInfoMember ( $class, "#oneToMany", $member ); |
83
|
1 |
|
if ($annot !== false) { |
84
|
|
|
$fkAnnot = OrmUtils::getAnnotationInfoMember ( $annot ["className"], "#joinColumn", $annot ["mappedBy"] ); |
85
|
|
|
if ($fkAnnot !== false) { |
86
|
|
|
var_dump ( $annot ); |
|
|
|
|
87
|
|
|
ob_end_flush (); |
88
|
|
|
$fkv = OrmUtils::getFirstKeyValue ( $instance ); |
89
|
|
|
$ret = self::_getAll ( $annot ["className"], ConditionParser::simple ( $fkAnnot ["name"] . "= ?", $fkv ), $included, $useCache ); |
90
|
|
|
if ($modifier = self::getAccessor ( $member, $instance, 'getOneToMany' )) { |
91
|
|
|
self::setToMember ( $member, $instance, $ret, $modifier ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
1 |
|
return $ret; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Assigns / loads the child records in the $member member of $instance. |
100
|
|
|
* If $array is null, the records are loaded from the database |
101
|
|
|
* |
102
|
|
|
* @param object $instance |
103
|
|
|
* @param string $member |
104
|
|
|
* Member on which a ManyToMany annotation must be present |
105
|
|
|
* @param boolean|array $included |
106
|
|
|
* if true, loads associate members with associations, if array, example : ["client.*","commands"] |
107
|
|
|
* @param array $array |
108
|
|
|
* optional parameter containing the list of possible child records |
109
|
|
|
* @param boolean $useCache |
110
|
|
|
*/ |
111
|
1 |
|
public static function getManyToMany($instance, $member, $included = false, $array = null, $useCache = NULL) { |
112
|
1 |
|
$ret = [ ]; |
113
|
1 |
|
$class = get_class ( $instance ); |
114
|
1 |
|
$parser = new ManyToManyParser ( $instance, $member ); |
115
|
1 |
|
if ($parser->init ()) { |
116
|
1 |
|
if (is_null ( $array )) { |
117
|
1 |
|
$accessor = "get" . ucfirst ( $parser->getMyPk () ); |
118
|
1 |
|
$condition = " INNER JOIN `" . $parser->getJoinTable () . "` on `" . $parser->getJoinTable () . "`.`" . $parser->getFkField () . "`=`" . $parser->getTargetEntityTable () . "`.`" . $parser->getPk () . "` WHERE `" . $parser->getJoinTable () . "`.`" . $parser->getMyFkField () . "`= ?"; |
119
|
1 |
|
$ret = self::_getAll ( $parser->getTargetEntityClass (), ConditionParser::simple ( $condition, $instance->$accessor () ), $included, $useCache ); |
120
|
|
|
} else { |
121
|
|
|
$ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser ); |
122
|
|
|
} |
123
|
1 |
|
if ($modifier = self::getAccessor ( $member, $instance, 'getManyToMany' )) { |
124
|
1 |
|
self::setToMember ( $member, $instance, $ret, $modifier ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
1 |
|
return $ret; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @param object $instance |
133
|
|
|
* @param array $array |
134
|
|
|
* @param boolean $useCache |
135
|
|
|
*/ |
136
|
|
|
public static function affectsManyToManys($instance, $array = NULL, $useCache = NULL) { |
137
|
|
|
$metaDatas = OrmUtils::getModelMetadata ( \get_class ( $instance ) ); |
138
|
|
|
$manyToManyFields = $metaDatas ["#manyToMany"]; |
139
|
|
|
if (\sizeof ( $manyToManyFields ) > 0) { |
140
|
|
|
foreach ( $manyToManyFields as $member ) { |
141
|
|
|
self::getManyToMany ( $instance, $member, false, $array, $useCache ); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns an array of $className objects from the database |
148
|
|
|
* |
149
|
|
|
* @param string $className |
150
|
|
|
* class name of the model to load |
151
|
|
|
* @param string $condition |
152
|
|
|
* Part following the WHERE of an SQL statement |
153
|
|
|
* @param boolean|array $included |
154
|
|
|
* if true, loads associate members with associations, if array, example : ["client.*","commands"] |
155
|
|
|
* @param array|null $parameters |
156
|
|
|
* @param boolean $useCache |
157
|
|
|
* use the active cache if true |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
5 |
|
public static function getAll($className, $condition = '', $included = true, $parameters = null, $useCache = NULL) { |
161
|
5 |
|
return self::_getAll ( $className, new ConditionParser ( $condition, null, $parameters ), $included, $useCache ); |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
public static function paginate($className, $page = 1, $rowsPerPage = 20, $condition = null, $included = true) { |
165
|
2 |
|
if (! isset ( $condition )) { |
166
|
1 |
|
$condition = "1=1"; |
167
|
|
|
} |
168
|
2 |
|
return self::getAll ( $className, $condition . " LIMIT " . $rowsPerPage . " OFFSET " . (($page - 1) * $rowsPerPage), $included ); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
public static function getRownum($className, $ids) { |
172
|
1 |
|
$tableName = OrmUtils::getTableName ( $className ); |
173
|
1 |
|
self::parseKey ( $ids, $className ); |
174
|
1 |
|
$condition = SqlUtils::getCondition ( $ids, $className ); |
175
|
1 |
|
$keyFields = OrmUtils::getKeyFields ( $className ); |
176
|
1 |
|
if (is_array ( $keyFields )) { |
177
|
1 |
|
$keys = implode ( ",", $keyFields ); |
178
|
|
|
} else { |
179
|
|
|
$keys = "1"; |
180
|
|
|
} |
181
|
1 |
|
return self::$db->queryColumn ( "SELECT num FROM (SELECT *, @rownum:=@rownum + 1 AS num FROM `{$tableName}`, (SELECT @rownum:=0) r ORDER BY {$keys}) d WHERE " . $condition ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
186
|
|
|
* |
187
|
|
|
* @param string $className |
188
|
|
|
* complete classname of the model to load |
189
|
|
|
* @param string $condition |
190
|
|
|
* Part following the WHERE of an SQL statement |
191
|
|
|
* @param array|null $parameters |
192
|
|
|
* The query parameters |
193
|
|
|
* @return int|false count of objects |
194
|
|
|
*/ |
195
|
3 |
|
public static function count($className, $condition = '', $parameters = null) { |
196
|
3 |
|
$tableName = OrmUtils::getTableName ( $className ); |
197
|
3 |
|
if ($condition != '') |
198
|
1 |
|
$condition = " WHERE " . $condition; |
199
|
3 |
|
return self::$db->prepareAndFetchColumn ( "SELECT COUNT(*) FROM `" . $tableName . "`" . $condition, $parameters ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns an instance of $className from the database, from $keyvalues values of the primary key |
204
|
|
|
* |
205
|
|
|
* @param String $className |
206
|
|
|
* complete classname of the model to load |
207
|
|
|
* @param Array|string $keyValues |
208
|
|
|
* primary key values or condition |
209
|
|
|
* @param boolean|array $included |
210
|
|
|
* if true, charges associate members with association |
211
|
|
|
* @param array|null $parameters |
212
|
|
|
* the request parameters |
213
|
|
|
* @param boolean|null $useCache |
214
|
|
|
* use cache if true |
215
|
|
|
* @return object the instance loaded or null if not found |
216
|
|
|
*/ |
217
|
6 |
|
public static function getOne($className, $keyValues, $included = true, $parameters = null, $useCache = NULL) { |
218
|
6 |
|
$conditionParser = new ConditionParser (); |
219
|
6 |
|
if (! isset ( $parameters )) { |
220
|
6 |
|
$conditionParser->addKeyValues ( $keyValues, $className ); |
221
|
|
|
} elseif (! is_array ( $keyValues )) { |
222
|
|
|
$conditionParser->setCondition ( $keyValues ); |
223
|
|
|
$conditionParser->setParams ( $parameters ); |
224
|
|
|
} else { |
225
|
|
|
throw new DAOException ( "The \$keyValues parameter should not be an array if \$parameters is not null" ); |
226
|
|
|
} |
227
|
6 |
|
return self::_getOne ( $className, $conditionParser, $included, $useCache ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Establishes the connection to the database using the past parameters |
232
|
|
|
* |
233
|
|
|
* @param string $dbType |
234
|
|
|
* @param string $dbName |
235
|
|
|
* @param string $serverName |
236
|
|
|
* @param string $port |
237
|
|
|
* @param string $user |
238
|
|
|
* @param string $password |
239
|
|
|
* @param array $options |
240
|
|
|
* @param boolean $cache |
241
|
|
|
*/ |
242
|
22 |
|
public static function connect($dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [], $cache = false) { |
243
|
22 |
|
self::$db = new Database ( $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache ); |
244
|
|
|
try { |
245
|
22 |
|
self::$db->connect (); |
246
|
|
|
} catch ( \Exception $e ) { |
247
|
|
|
Logger::error ( "DAO", $e->getMessage () ); |
248
|
|
|
throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () ); |
249
|
|
|
} |
250
|
22 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Establishes the connection to the database using the $config array |
254
|
|
|
* |
255
|
|
|
* @param array $config |
256
|
|
|
* the config array (Startup::getConfig()) |
257
|
|
|
*/ |
258
|
1 |
|
public static function startDatabase(&$config) { |
259
|
1 |
|
$db = $config ['database'] ?? [ ]; |
260
|
1 |
|
if ($db ['dbName'] !== '') { |
261
|
1 |
|
self::connect ( $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false); |
262
|
|
|
} |
263
|
1 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns true if the connection to the database is established |
267
|
|
|
* |
268
|
|
|
* @return boolean |
269
|
|
|
*/ |
270
|
4 |
|
public static function isConnected() { |
271
|
4 |
|
return self::$db !== null && (self::$db instanceof Database) && self::$db->isConnected (); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|