Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //******************************************************************************************************************
- /**
- * Builds a PDO prepared statement with the given parameters
- *
- * @param array $args
- * Holds the WHERE clause
- * Expected Format: array("Column Name" => array("Comparison Operator", "Value"))
- *
- * @param bool $fetchImmediately
- * Defines whether or not the value is to be fetched immediately. If given FALSE the prepared statement is returned instead.
- *
- * @param bool $alwaysFetchArray
- * Defines whether or not there's always an array to be returned, even if the executed statement would only return one row.
- * Handy when multiple results are expected but when single ones are possible as well.
- */
- public static function select($columns,
- $table,
- $args = array(),
- $alwaysFetchArray = false,
- $orderBy = "",
- $orderDirection = "ASC",
- $limit = "",
- $groupBy = "",
- $fetchImmediately = true)
- {
- //The base query string.
- $sql = "SELECT ".$columns." FROM ".$table;
- //Adds the WHERE-clause and the necessary placeholders if specified
- if ($args)
- {
- $sql = $sql." WHERE ";
- $columnNames = array_keys($args);
- $columnVals = array();
- for ($i = 0; $i < count($columnNames); $i++)
- {
- $compOperator = array_values($args)[$i][0];
- array_push($columnVals, array_values($args)[$i][1]);
- if ($i == 0)
- {
- $sql .= $columnNames[$i]." ".$compOperator." ?";
- continue;
- }
- $sql .= " AND ".$columnNames[$i]." ".$compOperator." ?";
- }
- }
- //Adds the GROUP BY-clause, if specified
- if ($groupBy)
- {
- $sql .= " GROUP BY ".$groupBy;
- }
- //Adds the ORDER BY-clause, if specified
- if ($orderBy)
- {
- $sql .= " ORDER BY ".$orderBy." ".$orderDirection;
- }
- //Adds the LIMIT-clause, if specified
- if ($limit)
- {
- $sql .= " LIMIT ".$limit;
- }
- //Prepares the built statement
- $stmt = self::$db->prepare($sql);
- //Executes the statement either with the specified values or without any.
- if (count($args))
- {
- $stmt->execute(array_values($columnVals));
- }
- else
- {
- $stmt->execute();
- }
- //Determines that the result is to be fetched as an instance of a class. Table names are expected as T[classname]
- $stmt->setFetchMode(PDO::FETCH_CLASS, substr($table, 1));
- if (!$fetchImmediately)
- {
- return $stmt;
- }
- return self::buildComplexObjectTree($stmt, $alwaysFetchArray);
- }
- //**********************************************************************************************************
- public static function buildComplexObjectTree($stmt, $alwaysFetchArray)
- {
- //Fetches the query result
- $result = $stmt->fetchAll();
- foreach ($result as $obj)
- {
- //Look through columns to find potential foreign keys columns.
- $fkCols = array();
- for ($i = 0; $i < $stmt->columnCount(); $i++)
- {
- $m = $stmt->getColumnMeta($i);
- if (str_starts_with($m['name'], "fk"))
- {
- array_push($fkCols, $m['name']);
- }
- }
- //Loops through all found foreign key columns and sets the respective values.
- foreach ($fkCols as $fkCol)
- {
- //Takes the fk columns name and returns substring representing the classname for the object
- $objName = substr($fkCol, 2);
- //Helps dealing with edge cases in which a table has multiple forein keys referencing the same table
- //e.g. private messages.
- //Expected Format: fkUserRecipient and fkUserSender
- if (preg_match("^(.*?[A-Z]){2,}.*$^", $objName))
- {
- $arr = preg_split('#([A-Z][^A-Z]*)#', $objName, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
- $objName = $arr[0];
- $propertyName = $arr[1];
- }
- //Holds the name of the setter function
- $setterFunc = isset($propertyName) ? "set".$propertyName : "set".$objName;
- //Holds the current foreign key value in the format expected from the select method
- $argArray = array("pk".$objName => array("=", $obj->$fkCol));
- //Queries the Database for the entry with the specified foreign key
- $obj->$setterFunc(Database::select("*", "T".$objName, $argArray));
- }
- if (count($result) == 1 && !$alwaysFetchArray)
- {
- return $obj;
- }
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment