Framework  3.9
IndexedPivotQuery Class Reference

Run a pivot query and return the results indexed by a selected field. More...

+ Inheritance diagram for IndexedPivotQuery:
+ Collaboration diagram for IndexedPivotQuery:

Public Member Functions

 __construct ($base, $baseConstraint="", $indexBy="")
 
 indexBy ($indexBy)
 Sets the field by which the results should be indexed. More...
 
 execute ()
 
- Public Member Functions inherited from AbstractPivotQuery
 __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...
 

Static Public Member Functions

static create ($base, $baseConstraint="", $indexBy="")
 Creates a new IndexedPivotQuery instance. More...
 

Public Attributes

 $indexBy
 
- Public Attributes inherited from AbstractPivotQuery
 $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
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractPivotQuery
 generateFieldExpressions ()
 Builds the expressions for the pivot query. More...
 
 generateQuery ()
 Generates SQL for the full pivot query, with grouping. More...
 

Detailed Description

Run a pivot query and return the results indexed by a selected field.

Author
andy

Definition at line 394 of file pivot.inc.

Constructor & Destructor Documentation

◆ __construct()

IndexedPivotQuery::__construct (   $base,
  $baseConstraint = "",
  $indexBy = "" 
)

Definition at line 398 of file pivot.inc.

399  {
400  parent::__construct($base, $baseConstraint);
401  $this->indexBy = $indexBy;
402  }
indexBy($indexBy)
Sets the field by which the results should be indexed.
Definition: pivot.inc:409

Member Function Documentation

◆ create()

static IndexedPivotQuery::create (   $base,
  $baseConstraint = "",
  $indexBy = "" 
)
static

Creates a new IndexedPivotQuery instance.

Parameters
string$baseclass name for the base objects
string$baseConstraintquery constraint for the base objects
string$indexBythe field by which the results should be indexed.
Returns
IndexedPivotQuery

Definition at line 473 of file pivot.inc.

474  {
475  return new IndexedPivotQuery($base, $baseConstraint, $indexBy);
476  }
Run a pivot query and return the results indexed by a selected field.
Definition: pivot.inc:395

◆ execute()

IndexedPivotQuery::execute ( )
See also
AbstractQuery::execute()

Reimplemented from AbstractQuery.

Definition at line 418 of file pivot.inc.

419  {
420  $query = $this->generateQuery();
421 
422  trace("$query", 3);
423  $items = array();
424  $field = $this->indexBy;
425 
426  try
427  {
429 
430  $result = $db->prepare($query);
431  $result->execute($this->params);
432 
433  while($line = $result->fetch())
434  {
435  $item = $this->createPivotItem();
436  $item->populate($line);
437  $val = $item->get($field);
438 
439  if (array_key_exists($val, $items))
440  {
441  // Implicitly promote to array if there is a collision
442  if (!is_array($items[$val]))
443  {
444  $items[$val] = array($items[$val]);
445  }
446  $items[$val][] = $item;
447  }
448  else
449  {
450  $items[$val] = $item;
451  }
452  }
453 
454  unset($result);
455  }
456  catch(PDOException $e)
457  {
458  $err = "IndexedPivotQuery failed - " . $e->getMessage();
459  trace($err, 2);
460  throw new DataItemException($err);
461  }
462 
463  return $items;
464  }
generateQuery()
Generates SQL for the full pivot query, with grouping.
Definition: pivot.inc:236
createPivotItem()
Creates an empty PivotItem, configured with fields and field aliases based on the pivot and range.
Definition: pivot.inc:279
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()

IndexedPivotQuery::indexBy (   $indexBy)

Sets the field by which the results should be indexed.

Parameters
string$indexBythe field by which the results should be indexed.
Returns
IndexedPivotQuery

Definition at line 409 of file pivot.inc.

410  {
411  $this->indexBy = $indexBy;
412  return $this;
413  }

Member Data Documentation

◆ $indexBy

IndexedPivotQuery::$indexBy

Definition at line 396 of file pivot.inc.


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