Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAO.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm;
4
23
34
39 public static $db;
40 public static $useTransformers = false;
41 public static $transformerOp = 'transform';
42 private static $conditionParsers = [ ];
43 protected static $modelsDatabase = [ ];
44
49 protected static $cache;
50
51 public static function getDb($model) {
52 return self::getDatabase ( self::$modelsDatabase [$model] ?? 'default');
53 }
54
65 public static function getAll($className, $condition = '', $included = true, $parameters = null, $useCache = NULL) {
66 $db = self::getDb ( $className );
67 return static::_getAll ( $db, $className, new ConditionParser ( $condition, null, $parameters ), $included, $useCache );
68 }
69
80 public static function getAllByIds($className, $keyValues = [ ], $included = true, $condition = '', $useCache = NULL): array {
81 $db = self::getDb ( $className );
82 $key = OrmUtils::getFirstKey ( $className );
83 $countK = \count ( $keyValues );
84 if ($countK > 0) {
85 $nCondition = $key . ' IN (' . \str_repeat ( '?,', $countK - 1 ) . '?)';
86 if ($condition != null) {
87 $nCondition .= ' AND ' . $condition;
88 }
89 return static::_getAll ( $db, $className, new ConditionParser ( $nCondition, null, $keyValues ), $included, $useCache );
90 }
91 return [];
92
93 }
94
95 public static function paginate($className, $page = 1, $rowsPerPage = 20, $condition = null, $included = true) {
96 return self::getAll ( $className, ($condition ?? '1=1') . ' LIMIT ' . $rowsPerPage . ' OFFSET ' . (($page - 1) * $rowsPerPage), $included );
97 }
98
111 public static function orderBy(string $className, string $field, string $order = 'ASC', string $condition = '', bool $included = true, $parameters = null, $useCache = NULL): array {
112 return self::getAll ( $className, ($condition ?? '1=1') . ' ORDER BY ' . $field . ' ' . $order, $included, $parameters, $useCache );
113 }
114
115 public static function getRownum($className, $ids) {
116 $tableName = OrmUtils::getTableName ( $className );
117 $db = self::getDb ( $className );
118 $quote = $db->quote;
119 self::parseKey ( $ids, $className, $quote );
120 $condition = SqlUtils::getCondition ( $ids, $className );
121 $keyFields = OrmUtils::getKeyFields ( $className );
122 if (\is_array ( $keyFields )) {
123 $keys = \implode ( ',', $keyFields );
124 } else {
125 $keys = '1';
126 }
127 return $db->getRowNum ( $tableName, $keys, $condition );
128 }
129
138 public static function count($className, $condition = '', $parameters = null) {
139 $tableName = OrmUtils::getTableName ( $className );
140 if ($condition != '') {
141 $condition = SqlUtils::checkWhere($condition);
142 }
143 $db = self::getDb ( $className );
144 $quote = $db->quote;
145 return $db->prepareAndFetchColumn ( 'SELECT COUNT(*) FROM ' . $quote . $tableName . $quote . $condition, $parameters );
146 }
147
156 public static function exists($className, $condition = '', $parameters = null): bool {
157 $tableName = OrmUtils::getTableName ( $className );
158 if ($condition != '') {
159 $condition = SqlUtils::checkWhere($condition);
160 }
161 $db = self::getDb ( $className );
162 $quote = $db->quote;
163 return (1 == $db->prepareAndFetchColumn ( "SELECT EXISTS(SELECT 1 FROM {$quote}{$tableName}{$quote}{$condition})", $parameters ));
164 }
165
177 public static function getOne($className, $condition, $included = true, $parameters = null, $useCache = NULL) {
178 $db = self::getDb ( $className );
179 $conditionParser = new ConditionParser ();
180 if (! isset ( $parameters )) {
181 $conditionParser->addKeyValues ( $condition, $className );
182 } elseif (! is_array ( $condition )) {
183 $conditionParser->setCondition ( $condition );
184 $conditionParser->setParams ( $parameters );
185 } else {
186 throw new DAOException ( "The \$condition parameter should not be an array if \$parameters is not null" );
187 }
188 return static::_getOne ( $db, $className, $conditionParser, $included, $useCache );
189 }
190
200 public static function getById($className, $keyValues, $included = true, $useCache = NULL) {
201 return static::_getOne ( self::getDatabase ( self::$modelsDatabase [$className] ?? 'default'), $className, self::getConditionParser ( $className, $keyValues ), $included, $useCache );
202 }
203
204 protected static function getConditionParser($className, $keyValues): ConditionParser {
205 if (! isset ( self::$conditionParsers [$className] )) {
206 $conditionParser = new ConditionParser ();
207 $conditionParser->addKeyValues ( $keyValues, $className );
208 self::$conditionParsers [$className] = $conditionParser;
209 } else {
210 self::$conditionParsers [$className]->setKeyValues ( $keyValues );
211 }
212 return self::$conditionParsers [$className];
213 }
214
230 public static function connect($offset, $wrapper, $dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [ ], $cache = false) {
231 self::$db [$offset] = new Database ( $wrapper, $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache, self::$pool );
232 try {
233 self::$db [$offset]->connect ();
234 } catch ( \Exception $e ) {
235 Logger::error ( "DAO", $e->getMessage () );
236 throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
237 }
238 }
239
245 public static function startDatabase(&$config, $offset = null) {
246 $db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']);
247 if ($db ['dbName'] !== '') {
248 self::connect ( $offset ?? 'default', $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false);
249 }
250 }
251
252 public static function getDbOffset(&$config, $offset = null) {
253 return $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']);
254 }
255
261 public static function isConnected($offset = 'default'): bool {
262 $db = self::$db [$offset] ?? false;
263 return ($db instanceof Database) && $db->isConnected ();
264 }
265
271 public static function setTransformerOp(string $op) {
272 self::$transformerOp = $op;
273 }
274
278 public static function closeDb($offset = 'default') {
279 $db = self::$db [$offset] ?? false;
280 if ($db !== false) {
281 $db->close ();
282 }
283 }
284
291 public static function setModelDatabase($model, $database = 'default') {
292 self::$modelsDatabase [$model] = $database;
293 }
294
300 public static function setModelsDatabases($modelsDatabase) {
301 self::$modelsDatabase = $modelsDatabase;
302 }
303
310 public static function getDatabase($offset = 'default') {
311 if (! isset ( self::$db [$offset] )) {
312 self::startDatabase ( Startup::$config, $offset );
313 }
314 SqlUtils::$quote = self::$db [$offset]->quote??'';
315 return self::$db [$offset]??null;
316 }
317
318 public static function getDatabases(): array {
319 $config = Startup::getConfig ();
320 if (isset ( $config ['database'] )) {
321 if (isset ( $config ['database'] ['dbName'] )) {
322 return [ 'default' ];
323 } else {
324 return \array_keys ( $config ['database'] );
325 }
326 }
327 return [ ];
328 }
329
330 public static function updateDatabaseParams(array &$config, array $parameters, $offset = 'default') {
331 if ($offset === 'default') {
332 if (isset ( $config ['database'] [$offset] )) {
333 foreach ( $parameters as $k => $param ) {
334 $config ['database'] [$offset] [$k] = $param;
335 }
336 } else {
337 foreach ( $parameters as $k => $param ) {
338 $config ['database'] [$k] = $param;
339 }
340 }
341 } else {
342 if (isset ( $config ['database'] [$offset] )) {
343 foreach ( $parameters as $k => $param ) {
344 $config ['database'] [$offset] [$k] = $param;
345 }
346 }
347 }
348 }
349
350 public static function start() {
351 self::$modelsDatabase = CacheManager::getModelsDatabases ();
352 }
353
354 public static function getDbCacheInstance($model) {
355 $db = static::$db [self::$modelsDatabase [$model] ?? 'default'];
356 return $db->getCacheInstance ();
357 }
358
359 public static function warmupCache($className, $condition = '', $included = false, $parameters = [ ]) {
360 $objects = self::getAll ( $className, $condition, $included, $parameters );
361 foreach ( $objects as $o ) {
362 self::$cache->store ( $className, OrmUtils::getKeyValues ( $o ), $o );
363 }
364 self::$cache->optimize ();
365 $offset = self::$modelsDatabase [$className] ?? 'default';
366 $db = self::$db [$offset];
367 $db->close ();
368 unset ( self::$db [$offset] );
369 }
370
371 public static function setCache(AbstractDAOCache $cache) {
372 self::$cache = $cache;
373 }
374
379 public static function getCache() {
380 return static::$cache;
381 }
382
386 public static function getModelsDatabase(): array {
387 return self::$modelsDatabase;
388 }
389}
Manager for caches (Router, Rest, models).
Ubiquity\cache\dao$AbstractDAOCache This class is part of Ubiquity.
Starts the framework.
Definition Startup.php:19
Ubiquity Generic database class.
Definition Database.php:25
SQL utilities.
Definition SqlUtils.php:13
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Gateway class between database and object model.
Definition DAO.php:33
static $conditionParsers
Definition DAO.php:42
static getConditionParser($className, $keyValues)
Definition DAO.php:204
static start()
Definition DAO.php:350
static count($className, $condition='', $parameters=null)
Returns the number of objects of $className from the database respecting the condition possibly passe...
Definition DAO.php:138
static $db
Definition DAO.php:39
static paginate($className, $page=1, $rowsPerPage=20, $condition=null, $included=true)
Definition DAO.php:95
static connect($offset, $wrapper, $dbType, $dbName, $serverName='127.0.0.1', $port='3306', $user='root', $password='', $options=[], $cache=false)
Establishes the connection to the database using the past parameters.
Definition DAO.php:230
static warmupCache($className, $condition='', $included=false, $parameters=[])
Definition DAO.php:359
static orderBy(string $className, string $field, string $order='ASC', string $condition='', bool $included=true, $parameters=null, $useCache=NULL)
Returns an array of $className objects from the database ordered by $field.
Definition DAO.php:111
static getDbOffset(&$config, $offset=null)
Definition DAO.php:252
static getOne($className, $condition, $included=true, $parameters=null, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key or wit...
Definition DAO.php:177
static closeDb($offset='default')
Closes the active pdo connection to the database.
Definition DAO.php:278
static getAllByIds($className, $keyValues=[], $included=true, $condition='', $useCache=NULL)
Returns an array of $className objects loaded by id from the database.
Definition DAO.php:80
static $transformerOp
Definition DAO.php:41
static getCache()
Definition DAO.php:379
static $modelsDatabase
Definition DAO.php:43
static $useTransformers
Definition DAO.php:40
static updateDatabaseParams(array &$config, array $parameters, $offset='default')
Definition DAO.php:330
static getDatabases()
Definition DAO.php:318
static startDatabase(&$config, $offset=null)
Establishes the connection to the database using the $config array.
Definition DAO.php:245
static getById($className, $keyValues, $included=true, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key.
Definition DAO.php:200
static getDatabase($offset='default')
Returns the database instance defined at $offset key in config.
Definition DAO.php:310
static exists($className, $condition='', $parameters=null)
Tests the existence of objects of $className from the database respecting the condition possibly pass...
Definition DAO.php:156
static setModelDatabase($model, $database='default')
Defines the database connection to use for $model class.
Definition DAO.php:291
static setModelsDatabases($modelsDatabase)
Defines the database connections to use for models classes.
Definition DAO.php:300
static getRownum($className, $ids)
Definition DAO.php:115
static $cache
Definition DAO.php:49
static getModelsDatabase()
Definition DAO.php:386
static setTransformerOp(string $op)
Sets the transformer operation.
Definition DAO.php:271
static getDb($model)
Definition DAO.php:51
static isConnected($offset='default')
Returns true if the connection to the database is established.
Definition DAO.php:261
static getDbCacheInstance($model)
Definition DAO.php:354
static setCache(AbstractDAOCache $cache)
Definition DAO.php:371
static getAll($className, $condition='', $included=true, $parameters=null, $useCache=NULL)
Returns an array of $className objects from the database.
Definition DAO.php:65
Object/relational mapping utilities.
Definition OrmUtils.php:17
Represents a query condition.
Ubiquity\orm\traits$DAOBulkUpdatesTrait This class is part of Ubiquity.
Ubiquity\orm\traits$DAOPreparedTrait This class is part of Ubiquity.
Used by DAO class, realize relations assignments.
Used by DAO class, prepare relations for loading.
Ubiquity\orm\traits$DAOUQueries This class is part of Ubiquity.
Class Configuration \config.