Framework  3.9
GroupedPivotQuery Class Reference

Performs a pivot query against the database, returning an array of arrays of PivotItems, grouped by a particular field. More...

+ Inheritance diagram for GroupedPivotQuery:
+ Collaboration diagram for GroupedPivotQuery:

Public Member Functions

 __construct ($base, $baseConstraint="", $groupBy="")
 
 groupBy ($groupBy)
 Set the field by which results should be grouped. 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 ($class, $constraints="", $groupBy="")
 Create a new GroupedPivotQuery. More...
 

Public Attributes

 $groupBy
 
- 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

Performs a pivot query against the database, returning an array of arrays of PivotItems, grouped by a particular field.

If the indexing field has unique values, indexedQuery() might be more appropriate.

Author
andy

Definition at line 486 of file pivot.inc.

Constructor & Destructor Documentation

◆ __construct()

GroupedPivotQuery::__construct (   $base,
  $baseConstraint = "",
  $groupBy = "" 
)

Definition at line 490 of file pivot.inc.

491  {
492  parent::__construct($base, $baseConstraint);
493  $this->groupBy = $groupBy;
494  }
groupBy($groupBy)
Set the field by which results should be grouped.
Definition: pivot.inc:501

Member Function Documentation

◆ create()

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

Create a new GroupedPivotQuery.

Parameters
string$classthe base class for the query
string$constraintsthe base constraint for the query
string$groupBythe field by which results should be grouped
Returns
GroupedPivotQuery

Definition at line 553 of file pivot.inc.

554  {
556  }
Performs a pivot query against the database, returning an array of arrays of PivotItems,...
Definition: pivot.inc:487

◆ execute()

GroupedPivotQuery::execute ( )
See also
AbstractQuery::execute()

Reimplemented from AbstractQuery.

Definition at line 510 of file pivot.inc.

511  {
512  $query = $this->generateQuery();
513 
514  trace($query, 3);
515 
516  $items = array();
517 
518  $field = $this->groupBy;
519 
520  try
521  {
523 
524  $result = $db->prepare($query);
525  $result->execute($this->params);
526 
527  while($line = $result->fetch())
528  {
529  $item = $this->createPivotItem();
530  $item->populate($line);
531  $items[$item->$field][] = $item;
532  }
533 
534  unset($result);
535  }
536  catch(PDOException $e)
537  {
538  $err = "GroupedPivotQuery failed - " . $e->getMessage();
539  trace($err, 2);
540  throw new DataItemException($err);
541  }
542 
543  return $items;
544  }
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

◆ groupBy()

GroupedPivotQuery::groupBy (   $groupBy)

Set the field by which results should be grouped.

Parameters
string$groupBythe field by which results should be grouped
Returns
GroupedPivotQuery

Definition at line 501 of file pivot.inc.

502  {
503  $this->groupBy = $groupBy;
504  return $this;
505  }

Member Data Documentation

◆ $groupBy

GroupedPivotQuery::$groupBy

Definition at line 488 of file pivot.inc.


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