Version: current

# Class Aggregate

Calculate an Aggregate on a particular field of a particular DataObject type (possibly with an additional filter before the aggregate)

Implemented as a class to provide a semi-DSL method of calculating Aggregates. DataObject has a function that will create & return an instance of this class with the DataObject type and filter set, but at that point we don't yet know the aggregate function or field

This class captures any XML_val or unknown call, and uses that to get the field & aggregate function & then return the result

Two ways of calling

$aggregate->XML_val(aggregate_function, array(field)) - For templates $aggregate->aggregate_function(field) - For PHP

Aggregate functions are uppercased by this class, but are otherwise assumed to be valid SQL functions. Some examples: Min, Max, Avg

Aggregates are often used as portions of a cacheblock key. They are therefore cached themselves, in the 'aggregate' cache, although the invalidation logic prefers speed over keeping valid data.

NOTE: The cache logic uses tags, and so a backend that supports tags is required. Currently only the File backend (and the two-level backend with the File backend as the slow store) meets this requirement

  Object
  ViewableData implements IteratorAggregate
  Aggregate

### Direct known subclasses

`Aggregate_Relationship`

Package: sapphire\core
Author: hfried
Located at sapphire/core/model/Aggregate.php

## Methods summary

`protected static` | # `cache( )` Build & cache the cache object Build & cache the cache object
`public static` | # `flushCache( mixed $class = null )` Clear the aggregate cache for a given type, or pass nothing to clear all aggregate caches Clear the aggregate cache for a given type, or pass nothing to clear all aggregate caches
`public` | # `__construct( string $type, string $filter = '' )` Constructor Constructor #### Parameters $type string $type The DataObject type we are building an aggregate for $filter string $filter (optional) An SQL filter to apply to the selected rows before calculating the aggregate #### Overrides `Object::__construct`
`protected `SQLQuery`` | # `query( string $attr )` Build the SQLQuery to calculate the aggregate This is a seperate function so that subtypes of Aggregate can change just this bit Build the SQLQuery to calculate the aggregate This is a seperate function so that subtypes of Aggregate can change just this bit #### Parameters $attr string $attr - the SQL field statement for selection (i.e. "MAX(LastUpdated)") #### Returns ``SQLQuery`` SQLQuery
`public string` | # `XML_val( string $name, array $args )` Entry point for being called from a template. Entry point for being called from a template. This gets the aggregate function #### Parameters $name string $field $args array $arguments #### Returns `string` string #### Overrides `ViewableData::XML_val`
`public mixed` | # `__call( string $method, array $arguments )` Entry point for being called from PHP. Entry point for being called from PHP. #### Parameters $method string $method $arguments array $arguments #### Returns `mixed` mixed #### Overrides `Object::__call`

### Methods inherited from ViewableData

`ATT_val()`, `BaseHref()`, `CSSClasses()`, `ColumnBreak()`, `ColumnCalc()`, `ColumnNumber()`, `ColumnPad()`, `ColumnPos()`, `CurrentMember()`, `CurrentPage()`, `Debug()`, `Even()`, `EvenOdd()`, `First()`, `FirstLast()`, `HasPerm()`, `IsAjax()`, `JS_val()`, `Last()`, `Me()`, `Middle()`, `MiddleString()`, `Modulus()`, `MultipleOf()`, `Odd()`, `Pos()`, `RAW_val()`, `SQL_val()`, `ThemeDir()`, `ThemeName()`, `Top()`, `TotalItems()`, `__get()`, `__isset()`, `__set()`, `buildCastingCache()`, `cachedCall()`, `castingClass()`, `castingHelper()`, `castingHelperPair()`, `castingObjectCreator()`, `castingObjectCreatorPair()`, `customise()`, `defineMethods()`, `escapeTypeForField()`, `getField()`, `getIterator()`, `getSecurityID()`, `getXMLValues()`, `hasField()`, `hasValue()`, `i18nLocale()`, `iteratorProperties()`, `obj()`, `renderWith()`, `setCustomisedObj()`, `setField()`

### Methods inherited from Object

`__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`public static mixed` | $cache | #

### Properties inherited from ViewableData

`$casting`, `$customisedObject`, `$default_cast`, `$failover`, `$iteratorPos`, `$iteratorTotalItems`

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -