Framework  3.9
AbstractPivotQuery Class Reference

AbstractPivotQuery provides the common base class for the shaped pivot query classes PivotQuery, IndexexPivotQuery and GroupedPivotQuery. More...

+ Inheritance diagram for AbstractPivotQuery:
+ Collaboration diagram for AbstractPivotQuery:

Public Member Functions

 __construct ($base, $baseConstraint="")
 Constructs a new AbstractPivotQuery. More...
 
 pivot ($pivotClass, $pivotConstraint, $pivotFunction="SUM")
 Adds the pivot to the query. More...
 
 range ($rangeClass, $rangeConstraint)
 Adds the range to the query. More...
 
 field ($field, $alias="", $type=String)
 Adds a fixed field to the output object. More...
 
 pivotField ($format, $value, $alias=null, $expression="SUM", $type=Number)
 Adds a pivot field format to the output object. More...
 
 createPivotItem ()
 Creates an empty PivotItem, configured with fields and field aliases based on the pivot and range. More...
 
 additionalFields ()
 Register additional fields from the base to be included in the PivotItem output. More...
 
- Public Member Functions inherited from AbstractQuery
 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...
 
 execute ()
 

Public Attributes

 $baseClass
 
 $baseConstraint
 
 $baseItem
 
 $rangeClass
 
 $rangeConstraint
 
 $pivotClass
 
 $pivotConstraint
 
 $pivotItem
 
 $fields
 
 $fieldAliases
 
 $additionalFields = array()
 
 $groupByFields = array()
 
 $orderBy
 
 $totalField = null
 
- Public Attributes inherited from AbstractQuery
 $class
 
 $constraints
 
 $filter
 
 $params
 

Protected Member Functions

 generateFieldExpressions ()
 Builds the expressions for the pivot query. More...
 
 generateQuery ()
 Generates SQL for the full pivot query, with grouping. More...
 

Detailed Description

AbstractPivotQuery provides the common base class for the shaped pivot query classes PivotQuery, IndexexPivotQuery and GroupedPivotQuery.

Every pivot consists of a 'base', 'range' and 'pivot'. The base is the root object, the pivot is the object that will be pivoted (i.e. have rows converted to columns in the output) and the range is the object that provides the range of values over which this conversion will occur.

Author
Andrew Green

Definition at line 50 of file pivot.inc.

Constructor & Destructor Documentation

◆ __construct()

AbstractPivotQuery::__construct (   $base,
  $baseConstraint = "" 
)

Constructs a new AbstractPivotQuery.

Parameters
string$baseClass name of the DataItem that will be the base
string$baseConstraintSQL constraint over which the base query will operate.

Reimplemented from AbstractQuery.

Reimplemented in PivotQuery.

Definition at line 78 of file pivot.inc.

79  {
80  $this->baseClass = $base;
81  $this->baseConstraint = $baseConstraint;
82 
83  $this->baseItem = new $this->baseClass;
84 
85  foreach($this->baseItem->getFields() as $field => $type)
86  {
87  $this->baseConstraint = preg_replace("/\\b".$field."\\b/i", "b.$field", $this->baseConstraint);
88  }
89 
90  $matches = array();
91 
92  if (preg_match("/ORDER BY.*/i", $this->baseConstraint, $matches))
93  {
94  $this->orderBy = $matches[0];
95  $this->baseConstraint = str_replace($this->orderBy, "", $this->baseConstraint);
96  }
97 
98  $this->pivotItem = null;
99 
100  $this->page = -1;
101  $this->size = -1;
102 
103  $basePK = $this->baseItem->getPrimaryKey();
104  $this->fields[$basePK] = $this->baseItem->getType($basePK);
105 
106  $this->additionalFields[] = "b.{$basePK} as {$basePK}";
107  $this->groupByFields[] = "b.{$basePK}";
108  }
additionalFields()
Register additional fields from the base to be included in the PivotItem output.
Definition: pivot.inc:287

Member Function Documentation

◆ additionalFields()

AbstractPivotQuery::additionalFields ( )

Register additional fields from the base to be included in the PivotItem output.

Definition at line 287 of file pivot.inc.

288  {
289  for($i = 0; $i < func_num_args(); ++$i)
290  {
291  $field = func_get_arg($i);
292  $type = $this->baseItem->getType($field);
293  if ($type)
294  {
295  $this->fields[$field] = $type;
296  $this->additionalFields[] = "b.{$field} as {$field}";
297  }
298  }
299  return $this;
300  }

◆ createPivotItem()

AbstractPivotQuery::createPivotItem ( )

Creates an empty PivotItem, configured with fields and field aliases based on the pivot and range.

Returns
PivotItem

Definition at line 279 of file pivot.inc.

280  {
281  return new PivotItem($this->fields, $this->baseClass, $this->baseItem->getPrimaryKey(), $this->fieldAliases);
282  }
PivotItems are dynamically constructed DataItems that map the results of PivotQueries.
Definition: pivot.inc:568

◆ field()

AbstractPivotQuery::field (   $field,
  $alias = "",
  $type = String 
)

Adds a fixed field to the output object.

These fields can be from the base object or the pivot object. If they are from the pivot object, then the resulting output will be subgrouped by each fixed field.

Parameters
string$fieldfield in the format "Object.field"
string$aliasAlias name for inclusion as labels or column headings in output
string$typeType of the field
Returns
AbstractPivotQuery

Definition at line 154 of file pivot.inc.

155  {
156  $field = preg_replace("/\\b".$this->baseClass."\\b/i", "b", $field);
157  $field = preg_replace("/\\b".$this->pivotClass."\\b/i", "p", $field);
158 
159  $bareField = preg_replace("/^.*?\\./", "", $field);
160 
161  $this->fields[$bareField] = String;
162  $this->additionalFields[] = "$field as $bareField";
163  $this->groupByFields[] = $field;
164 
165  return $this;
166  }

◆ generateFieldExpressions()

AbstractPivotQuery::generateFieldExpressions ( )
protected

Builds the expressions for the pivot query.

Exceptions
FakoliExpression
Returns
Array

Definition at line 192 of file pivot.inc.

193  {
194  if (!$this->rangeClass)
195  {
196  if (!$this->pivotClass)
197  {
198  throw new FakoliExpression("No Range or Pivot class provided");
199  }
200 
201  $this->rangeClass = $this->pivotClass;
202  $this->rangeConstaint = $this->pivotConstraint;
203  }
204 
205  $obj = new $this->rangeClass;
206  $pivot = $obj->getPrimaryKey();
207 
208  $rangeItems = Query::create($this->rangeClass, $this->rangeConstraint)->execute();
209 
210  $expressionMap = array();
211 
212  foreach($rangeItems as $item)
213  {
214  foreach($this->fieldNameFormats as $fieldName => $defn)
215  {
216  $fieldName = $item->format($fieldName);
217  $value = $defn["value"];
218  $expression = $defn["expression"];
219  $alias = $defn["alias"];
220 
221  $empty = ($defn["type"] == String) ? "''" : 0;
222 
223  $expressionMap[$fieldName] = "$expression(IF (p.{$pivot}=".$item->quoteFieldValue($pivot).", IFNULL(p.{$value}, {$empty}), {$empty}))";
224  $this->fields[$fieldName] = $defn["type"];
225  $this->fieldAliases[$fieldName] = $item->format($alias);
226  }
227  }
228 
229  return $expressionMap;
230  }
static create($class, $constraints="")
Static factory method to create a new Query.
Definition: query.inc:358

◆ generateQuery()

AbstractPivotQuery::generateQuery ( )
protected

Generates SQL for the full pivot query, with grouping.

Returns
string

Definition at line 236 of file pivot.inc.

237  {
238  // First generate fields
239 
240  $expressionMap = $this->generateFieldExpressions();
241 
242  $additionalFields = implode(", ", $this->additionalFields);
243  $groupByFields = implode(", ", $this->groupByFields);
244 
245  $base = $this->baseItem;
246  $pivot = $this->pivotItem;
247 
248  $basePK = $base->getPrimaryKey();
249  $pivotPK = $pivot->getPrimaryKey();
250 
251  $query = "SELECT $additionalFields";
252  foreach($expressionMap as $field => $expression)
253  {
254  $query .= ", ";
255  $query .= "\n\t{$expression} as $field";
256  $first = false;
257  }
258 
259  $query .= "\nFROM {$base->table} b left outer join {$pivot->table} p \n";
260 
261  $query .= "ON (p.{$basePK}=b.{$basePK} ";
262 
263  $constraint = preg_replace("/^\\s*WHERE /i", "AND ", $this->pivotConstraint);
264  $query .= " ".$constraint;
265 
266  $constraint = $this->baseConstraint ? $this->baseConstraint : "";
267  $query .= ") $constraint GROUP BY $groupByFields";
268  $query .= " ".$this->orderBy;
269 
270  $this->query = $query;
271  return $query;
272  }
generateFieldExpressions()
Builds the expressions for the pivot query.
Definition: pivot.inc:192
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
Definition: query.inc:373

◆ pivot()

AbstractPivotQuery::pivot (   $pivotClass,
  $pivotConstraint,
  $pivotFunction = "SUM" 
)

Adds the pivot to the query.

Parameters
string$pivotClassthe class of DataItem that will be the pivot
string$pivotConstraintSQL constraint over which the pivot query will operate
string$pivotFunctiondefault SQL aggregation function used to reduce rows when pivoting (default is SUM, but MAX, MIN, AVG, etc. can also be useful)
Returns
AbstractPivotQuery

Definition at line 117 of file pivot.inc.

118  {
119  $this->pivotClass = $pivotClass;
120  $this->pivotConstraint = $pivotConstraint;
121 
122  $this->pivotItem = new $this->pivotClass;
123 
124  foreach($this->pivotItem->getFields() as $field => $type)
125  {
126  $this->pivotConstraint = preg_replace("/\\b".$field."\\b/i", "p.$field", $this->pivotConstraint);
127  }
128 
129  return $this;
130  }

◆ pivotField()

AbstractPivotQuery::pivotField (   $format,
  $value,
  $alias = null,
  $expression = "SUM",
  $type = Number 
)

Adds a pivot field format to the output object.

One matching output field will be added for each entry in the range.

Parameters
string$formatformat specifier for the output field (i.e. "pivot_{pivot_id}")
string$valuevalue expression that the field will be populated with. This could be simply a field in the pivot, or it might be a more complex SQL expression
string$aliasAlias name for inclusion as labels or column headings in output
string$expressionSQL aggregation function used to reduce rows when pivoting (default is SUM, but MAX, MIN, AVG, etc. can also be useful)
string$typeType of the field
Returns
AbstractPivotQuery

Definition at line 179 of file pivot.inc.

180  {
181  if (!$alias) $alias = $format;
182 
183  $this->fieldNameFormats[$format] = array("expression" => $expression, "alias" => $alias, "value" => $value, "type" => $type);
184  return $this;
185  }

◆ range()

AbstractPivotQuery::range (   $rangeClass,
  $rangeConstraint 
)

Adds the range to the query.

Parameters
string$rangeClassthe class of DataItem that will be supplying the range for the pivot
string$rangeConstraintSQL constraint over which the range will operate
Returns
AbstractPivotQuery

Definition at line 138 of file pivot.inc.

139  {
140  $this->rangeClass = $rangeClass;
141  $this->rangeConstraint = $rangeConstraint;
142  return $this;
143  }

Member Data Documentation

◆ $additionalFields

AbstractPivotQuery::$additionalFields = array()

Definition at line 67 of file pivot.inc.

◆ $baseClass

AbstractPivotQuery::$baseClass

Definition at line 52 of file pivot.inc.

◆ $baseConstraint

AbstractPivotQuery::$baseConstraint

Definition at line 53 of file pivot.inc.

◆ $baseItem

AbstractPivotQuery::$baseItem

Definition at line 54 of file pivot.inc.

◆ $fieldAliases

AbstractPivotQuery::$fieldAliases

Definition at line 65 of file pivot.inc.

◆ $fields

AbstractPivotQuery::$fields

Definition at line 64 of file pivot.inc.

◆ $groupByFields

AbstractPivotQuery::$groupByFields = array()

Definition at line 68 of file pivot.inc.

◆ $orderBy

AbstractPivotQuery::$orderBy

Definition at line 70 of file pivot.inc.

◆ $pivotClass

AbstractPivotQuery::$pivotClass

Definition at line 59 of file pivot.inc.

◆ $pivotConstraint

AbstractPivotQuery::$pivotConstraint

Definition at line 60 of file pivot.inc.

◆ $pivotItem

AbstractPivotQuery::$pivotItem

Definition at line 62 of file pivot.inc.

◆ $rangeClass

AbstractPivotQuery::$rangeClass

Definition at line 56 of file pivot.inc.

◆ $rangeConstraint

AbstractPivotQuery::$rangeConstraint

Definition at line 57 of file pivot.inc.

◆ $totalField

AbstractPivotQuery::$totalField = null

Definition at line 71 of file pivot.inc.


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