Class Aggregate
Provides streamlined access to a virtually unlimited number of data fields for a pre-defined conditional. Different data fields are actually stored as separate rows with the field name being a value of the configured column key.
Tabular storage has a similar structure as follows:
ID | Key | Value1 | Value2 | ... |
---|---|---|---|---|
1 | A | TextA1 | TextA2 | ... |
1 | B | TextB1 | TextB2 | ... |
1 | C | TextC1 | TextC2 | ... |
An aggregator is created with a conditional that requires ID = 1
. The
key column name is set to Key
. The resulting instance allows simple data
field access to the fields A
, B
and C
which all yield an array with
indices Value1
and Value2
, each with their text values from the example
table columns, respectively.
If a new virtual data field D
is set, the aggregator will automatically
create a new storage entry with Key
set to D
for ID
being set to 1
.
If no access to multiple values per data field is required, subclasses can
simply overwrite the getter and setter methods to instead work with a single
value. An example implementation of a model that allows an arbitrary number
of settings for a fixed user_id
is as follows. The storage back-end table
has only three columns, user_id
, key
and value
:
class UserSettings extends \Core\Storage\Field\Aggregate { const ReferenceName = 'user_settings'; const ValueName = 'value'; public function __construct($userID) { parent::__construct( // Every user has a unique user ID. db::eq(db::key('user_id'), $userID), // The user ID is the only template value required to add new entries. array ( 'user_id' => $userID ), ); } public function getDataForKey($key) { $valueSet = parent::getDataForUndefinedKey($key); if (isset ($valueSet[static::ValueName])) { return $valueSet[static::ValueName]; } return null; } public function setDataForKey($key, $value) { return parent::setDataForKey($key, array ( static::ValueName => $value )); } }
- Core\Object implements Core\Chainable
- Core\Set implements Core\Accessor
- Core\Set\Mutable implements Core\Mutator
- Core\Field implements Core\Inquiry
- Core\Field\Mutable implements Core\Variator
- Core\Storage\Field
- Core\Storage\Field\Aggregate
Namespace: Core\Storage\Field
Package: Core\Storage
Since: 3.1
Requires: PHP 5.3
Version: 1.0
Located at Storage/Field/Aggregate.inc.php
Methods summary
public
|
|
public static
|
|
public
array
|
|
public
boolean
|
|
public
true|
|
#
setDataForKey( string $key, array $values )
Sets a set of virtual values and writes them to the storage back-end. |
public
|
#
setDataForUndefinedKey( string $key, array $values )
Sets a virtual set of values by adding a new entry at the back-end. |
public
|
#
unsetDataForKey( $key )
Removed all values associated with the aggregate key (ie. the whole row in the back-end storage). |
Methods inherited from Core\Storage\Field
Methods inherited from Core\Field\Mutable
offsetSet()
,
offsetUnset()
,
setDataForKeys()
,
setKeyMasquerade()
,
updateMasqueradedKeys()
Methods inherited from Core\Field
getDataForKey()
,
getDataForKeys()
,
offsetExists()
,
offsetGet()
Methods inherited from Core\Set\Mutable
Methods inherited from Core\Object
__autocreateFactory()
,
__call()
,
__processParameters()
,
__toString()
,
attachMethod()
,
chain()
,
getValueForKey()
,
getValueForKeyPath()
,
getValueForUndefinedKey()
,
hash()
,
issetValueForKey()
,
setValueForKey()
,
setValueForKeyPath()
,
setValueForUndefinedKey()
,
uuid()
Constants summary
string |
KeyColumnName
Name of column that holds the field keys. A derived class should overwrite this value with the index name of the data key, if different from "key". |
#
'key'
|
string |
ReferenceName
Name of storage reference. A derived class must overwrite this value to the name of the reference the element is stored under. |
#
'__notSet'
|
string |
StorageName
Name of storage object in global registry. A derived class should overwrite this value to the name of a registered storage driver instance, if it differs from "db". |
#
'db'
|
Constants inherited from Core\Storage\Field
Constants inherited from Core\Object
AnyParameterType
,
AutochainParameterType
,
AutocreateParameterType
,
BooleanParameterType
,
CharParameterType
,
EnumParameterType
,
IntegerParameterType
,
RealParameterType
,
StringParameterType
,
UserParameterType
Properties summary
protected
array
|
$_v_initialSet
Initial data array used as a template for new related storage entries. |
Properties inherited from Core\Storage\Field
$_v_cacheMode
,
$_v_conditional
,
$_v_driver
,
$_v_reference
,
$_v_virtualData
Properties inherited from Core\Field\Mutable
Properties inherited from Core\Field
Magic properties
public read-only
array
|
$initialSet
Initial data array used as a template for new storage entries. |