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

Public Member Functions

 __construct ($class, $constraints="", $groupBy="")
 
 groupBy ($groupBy)
 
 execute ()
 
 executeValue ($func)
 Query the database to calculate aggregate values by grouping. More...
 
- 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="", $groupBy="")
 

Public Attributes

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

Detailed Description

Definition at line 39 of file grouped_query.inc.

Constructor & Destructor Documentation

◆ __construct()

GroupedQuery::__construct (   $class,
  $constraints = "",
  $groupBy = "" 
)

Definition at line 43 of file grouped_query.inc.

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

Member Function Documentation

◆ create()

static GroupedQuery::create (   $class,
  $constraints = "",
  $groupBy = "" 
)
static

Definition at line 159 of file grouped_query.inc.

160  {
162  }

◆ execute()

GroupedQuery::execute ( )

Reimplemented from AbstractQuery.

Definition at line 55 of file grouped_query.inc.

56  {
57  $prototype = new $this->class;
58 
59  if (!$this->groupBy) $this->groupBy = $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"; //TODO - tidy this up some day
77 
78  $query = "SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint()." $orderBy";;
79  trace($query, 3);
80 
81  $items = array();
82 
83  $field = $this->groupBy;
84 
85  $useFormat = preg_match("/^[A-Za-z0-9_]*$/", $field) ? false : true;
86 
87  try
88  {
90 
91  $result = $db->prepare($query);
92  $result->execute($this->params);
93 
94  while($line = $result->fetch())
95  {
96  $item = new $this->class; //Hack to work around PHP's stupid implementation of get_class()
97  $item->populate($line);
98  $val = ($useFormat) ? $item->format($field) : $item->get($field);
99 
100  $items[$val][] = $item;
101  }
102 
103  unset($result);
104  }
105  catch(PDOException $e)
106  {
107  $err = "groupedQuery() failed - " . $e->getMessage();
108  trace($err, 2);
109  throw new DataItemException($err);
110  }
111 
112  return $items;
113  }
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

◆ executeValue()

GroupedQuery::executeValue (   $func)

Query the database to calculate aggregate values by grouping.

The database table associated with the specified class is used as the source for the data.

Parameters
string$functhe value or function to retrieve
Returns
array an associative array of group values to calculated aggregate values

Definition at line 122 of file grouped_query.inc.

123  {
124  $prototype = new $this->class;
125 
126  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
127  $this->constraints .= " ".$prototype->getIdentityConstraint();
128 
129  $query = "SELECT {$this->groupBy}, $func as result FROM {$prototype->table} {$this->tableAlias} {$this->constraints} GROUP BY {$this->groupBy}";
130 
131  trace($query, 3);
132 
133  $values = array();
134 
135  try
136  {
138 
139  $result = $db->prepare($query);
140  $result->execute($this->params);
141 
142  while ($row = $result->fetch())
143  {
144  $values[$row[$this->groupBy]] = $row['result'];
145  }
146 
147  unset($result);
148  }
149  catch(PDOException $e)
150  {
151  $err = "Query::executeValue() failed - " . $e->getMessage();
152  trace($err, 2);
153  throw new DataItemException($err);
154  }
155 
156  return $values;
157  }

◆ groupBy()

GroupedQuery::groupBy (   $groupBy)

Definition at line 49 of file grouped_query.inc.

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

Member Data Documentation

◆ $groupBy

GroupedQuery::$groupBy

Definition at line 41 of file grouped_query.inc.


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