Framework  3.9
IndexedQuery Class Reference
+ Inheritance diagram for IndexedQuery:
+ Collaboration diagram for IndexedQuery:

Public Member Functions

 __construct ($class, $constraints="", $indexBy="")
 
 indexBy ($indexBy)
 
 execute ()
 
- Public Member Functions inherited from AbstractQuery
 __construct ($class, $constraints="")
 
 constraints ($constraints)
 Sets the constraint clause for the Query. More...
 
 filter ($filter)
 Sets a filter to constrain the fields retrieved when the query is executed. More...
 
 params ($params)
 Sets the bound parameters array. More...
 
 bind ()
 Binds placeholders to parameter values. More...
 

Static Public Member Functions

static create ($class, $constraints="", $indexBy="")
 

Public Attributes

 $indexBy
 
- Public Attributes inherited from AbstractQuery
 $class
 
 $constraints
 
 $filter
 
 $params
 

Detailed Description

Definition at line 39 of file indexed_query.inc.

Constructor & Destructor Documentation

◆ __construct()

IndexedQuery::__construct (   $class,
  $constraints = "",
  $indexBy = "" 
)

Definition at line 43 of file indexed_query.inc.

44  {
45  parent::__construct($class, $constraints);
46  $this->indexBy = $indexBy;
47  }
indexBy($indexBy)

Member Function Documentation

◆ create()

static IndexedQuery::create (   $class,
  $constraints = "",
  $indexBy = "" 
)
static

Definition at line 127 of file indexed_query.inc.

128  {
130  }

◆ execute()

IndexedQuery::execute ( )

Reimplemented from AbstractQuery.

Definition at line 55 of file indexed_query.inc.

56  {
57  $prototype = new $this->class;
58 
59  if (!$this->indexBy) $this->indexBy = $prototype->getPrimaryKey();
60 
61  if ($this->filter)
62  {
63  $prototype->filter = $this->filter;
64  }
65 
66  $order_by_idx = strpos(strtoupper($this->constraints), "ORDER BY");
67  if ($order_by_idx !== false)
68  {
69  $orderBy = substr($this->constraints, $order_by_idx);
70 
71  $this->constraints = substr($this->constraints, 0, $order_by_idx);
72  }
73 
74  $fields = $prototype->getFieldList();
75 
76  if ($this->constraints == "") $this->constraints = "WHERE 1=1";
77  $query = "SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint()." $orderBy";
78  trace("DataItem indexedQuery(): $query", 3);
79 
80  $items = array();
81 
82  $field = $this->indexBy;
83 
84  $useFormat = preg_match("/^[A-Za-z0-9_]*$/", $field) ? false : true;
85 
86  try
87  {
89 
90  $result = $db->prepare($query);
91  $result->execute($this->params);
92 
93  while($line = $result->fetch())
94  {
95  $item = new $this->class;
96  $item->populate($line);
97 
98  $val = ($useFormat) ? $item->format($field) : $item->get($field);
99 
100  if (array_key_exists($val, $items))
101  {
102  // Implicitly promote to array if there is a collision
103  if (!is_array($items[$val]))
104  {
105  $items[$val] = array($items[$val]);
106  }
107  $items[$val][] = $item;
108  }
109  else
110  {
111  $items[$val] = $item;
112  }
113  }
114 
115  unset($result);
116  }
117  catch(PDOException $e)
118  {
119  $err = "indexedQuery() failed - " . $e->getMessage();
120  trace($err, 2);
121  throw new DataItemException($err);
122  }
123 
124  return $items;
125  }
constraints($constraints)
Sets the constraint clause for the Query.
Definition: query.inc:68
filter($filter)
Sets a filter to constrain the fields retrieved when the query is executed.
Definition: query.inc:79
params($params)
Sets the bound parameters array.
Definition: query.inc:91
static getConnection()
Retrieves a reference to the global database connection.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ indexBy()

IndexedQuery::indexBy (   $indexBy)

Definition at line 49 of file indexed_query.inc.

50  {
51  $this->indexBy = $indexBy;
52  return $this;
53  }

Member Data Documentation

◆ $indexBy

IndexedQuery::$indexBy

Definition at line 41 of file indexed_query.inc.


The documentation for this class was generated from the following file: