/** * * @param PCMapper $mapper * @param PCDatabaseQueryCondition $condition * @param PCDatabaseQueryLimit $limit * @param PCDatabaseQueryOrder $oder */ public static function withMapper($mapper, $condition = NULL, $limit = NULL, $order = NULL) { return new PCDatabaseSelectQuery($mapper->getTableName(), $mapper->getRequiredAttributes(), $condition, $limit, $order); }
/** * Returns an array of instanced subclass of PCModelObject according to $name * @param PCMapper $mapper a subclass of PCMapper * @param array $keys a key value array of elements for filtering search in database * @param array $optionalAttributes an array of strings containing the names of the optional fields to fetch * @param boolean $useCache if use the cache or not * @param string $limit the sql query limit * @param string $order the order specification * @return array an array of instanced subclass of PCModelObject according to $name */ public static function fetchModelObjectInstances($mapper, $keys, $optionalAttributes = null, $useCache = false, $limit = null, $order = NULL) { /* if(empty($keys)){ throw new Exception("Illegal argument, \"keys\" is empty"); } */ if ($mapper == null) { throw new Exception("Mapper must not be null"); } $required_field = $mapper->getRequiredAttributes(); if ($useCache && isset($keys['identifier'])) { $identifier = $keys['identifier']; $cache = PCCache::cacheProvider(); //error_log("GETTING : " . $mapper->getCacheKey($keys)); $item = $cache->getItem($mapper->getCacheKey($keys)); if (isset($item) && $item !== FALSE) { //error_log("GETTED : " . $mapper->getCacheKey($keys)); return $item; } } $fields = NULL; //unisco parametri opzionali (se presenti) a parametri richiesti if ($optionalAttributes == null) { $fields = $required_field; } else { $fields = array_merge($optionalAttributes, $required_field); } //creo stringa da parametri separati da virgola $fields_string = implode(",", $fields); $table_name = $mapper->getTableName(); $select_stm = "SELECT " . $fields_string . " FROM " . $table_name . " WHERE "; $prepared_keys_array = array(); $count = 0; $tot = count($keys); foreach ($keys as $key => $value) { $count++; $placeHolder = ":{$key}"; $select_stm .= $count == $tot ? " {$key} = {$placeHolder} " : " {$key} = {$placeHolder} AND "; $prepared_keys_array[$placeHolder] = $value; } if ($tot == NULL) { $select_stm .= " TRUE"; } if (isset($order)) { $select_stm .= ' ORDER BY ' . $order; } if (isset($limit)) { $select_stm .= ' LIMIT ' . $limit; } $pdo = PCDatabase::getSharedDatabaseConnection(); $prepared = $pdo->prepare($select_stm); if ($prepared->execute($prepared_keys_array) == FALSE) { $message = "Errore database: (" . $prepared->errorCode() . ") " . $prepared->errorInfo()[1] . " " . $prepared->errorInfo()[2]; throw new Exception($message); } $result = null; $toReturn = array(); while (($result = $prepared->fetch(PDO::FETCH_ASSOC)) != NULL) { $toReturn[] = $mapper->getMappedInstance($result); } if ($useCache && count($toReturn) == 1) { $identifier = $keys['identifier']; //error_log("STORING: " . $mapper->getCacheKey($keys)); PCCache::cacheProvider()->setItem($toReturn, $mapper->getCacheKey($keys), 300); } $prepared = NULL; return $toReturn; }
/** * * @param PCMapper $mapper * @param array $values * @param PCDatabaseQueryCondition $condition * @return PCDatabaseUpdateQuery */ public static function withMapper($mapper, $values, $condition = NULL) { return new PCDatabaseUpdateQuery($mapper->getTableForInsertUpdate(), $values, $condition); }