37 require_once realpath(dirname(__FILE__).
"/query.inc");
66 $order_by_idx = strpos(strtoupper($this->
constraints),
"ORDER BY");
67 if ($order_by_idx !==
false)
69 $orderBy = substr($this->
constraints, $order_by_idx);
74 $fields = $prototype->getFieldList();
77 $query =
"SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint().
" $orderBy";
78 trace(
"DataItem indexedQuery(): $query", 3);
84 $useFormat = preg_match(
"/^[A-Za-z0-9_]*$/", $field) ? false :
true;
90 $result = $db->prepare($query);
91 $result->execute($this->
params);
93 while($line = $result->fetch())
96 $item->populate($line);
98 $val = ($useFormat) ? $item->format($field) : $item->get($field);
100 if (array_key_exists($val, $items))
103 if (!is_array($items[$val]))
105 $items[$val] = array($items[$val]);
107 $items[$val][] = $item;
111 $items[$val] = $item;
117 catch(PDOException $e)
119 $err =
"indexedQuery() failed - " . $e->getMessage();
147 $field = $prototype->primary_key;
152 if (func_num_args() > 1)
154 $query->constraints(func_get_arg(1));
156 if (func_num_args() > 2)
158 $query->indexBy(func_get_arg(2));
160 if (func_num_args() > 3)
162 $query->filter(func_get_arg(3));
167 return $query->execute();
constraints($constraints)
Sets the constraint clause for the Query.
filter($filter)
Sets a filter to constrain the fields retrieved when the query is executed.
params($params)
Sets the bound parameters array.
static getConnection()
Retrieves a reference to the global database connection.
__construct($class, $constraints="", $indexBy="")
static create($class, $constraints="", $indexBy="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
indexedQuery($class)
Performs a query against the database, returning an array of DataItem objects of the specified class,...