1 <?php
2
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
32 class Aggregate extends ViewableData {
33
34 static $cache = null;
35
36
37 protected static function cache() {
38 return self::$cache ? self::$cache : (self::$cache = SS_Cache::factory('aggregate'));
39 }
40
41
42 public static function flushCache($class=null) {
43 $cache = self::cache();
44
45 if (!$class || $class == 'DataObject') {
46 $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('aggregate'));
47 }
48 else {
49 $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, ClassInfo::ancestry($class));
50 }
51 }
52
53 54 55 56 57 58
59 public function __construct($type, $filter = '') {
60 $this->type = $type;
61 $this->filter = $filter;
62 parent::__construct();
63 }
64
65 66 67 68 69 70
71 protected function query($attr) {
72 $singleton = singleton($this->type);
73 $query = $singleton->buildSQL($this->filter);
74 $query->select = array($attr);
75 $query->orderby = null;
76 $singleton->extend('augmentSQL', $query);
77 return $query;
78 }
79
80 81 82 83 84 85
86 public function XML_val($name, $args) {
87 $func = strtoupper( strpos($name, 'get') === 0 ? substr($name, 3) : $name );
88 $attribute = $args ? $args[0] : 'ID';
89
90 $table = null;
91
92 foreach (ClassInfo::ancestry($this->type, true) as $class) {
93 $fields = DataObject::database_fields($class);
94 if (array_key_exists($attribute, $fields)) { $table = $class; break; }
95 }
96
97 if (!$table) user_error("Couldn't find table for field $attribute in type {$this->type}", E_USER_ERROR);
98
99 $query = $this->query("$func(\"$table\".\"$attribute\")");
100
101 $cachekey = sha1($query->sql());
102 $cache = self::cache();
103
104 if (!($result = $cache->load($cachekey))) {
105 $result = (string)$query->execute()->value(); if (!$result) $result = '0';
106 $cache->save($result, null, array('aggregate', $this->type));
107 }
108
109 return $result;
110 }
111
112 113 114
115 public function __call($method, $arguments) {
116 return $this->XML_val($method, $arguments);
117 }
118 }
119
120 121 122 123 124 125 126
127 class Aggregate_Relationship extends Aggregate {
128
129 130 131 132 133 134 135
136 public function __construct($object, $relationship, $filter = '') {
137 $this->object = $object;
138 $this->relationship = $relationship;
139
140 $this->has_many = $object->has_many($relationship);
141 $this->many_many = $object->many_many($relationship);
142
143 if (!$this->has_many && !$this->many_many) user_error("Could not find relationship $relationship on object class {$object->class} in Aggregate Relationship", E_USER_ERROR);
144
145 parent::__construct($this->has_many ? $this->has_many : $this->many_many[1], $filter);
146 }
147
148 protected function query($attr) {
149 if ($this->has_many) {
150 $query = $this->object->getComponentsQuery($this->relationship, $this->filter);
151 }
152 else {
153 $query = $this->object->getManyManyComponentsQuery($this->relationship, $this->filter);
154 }
155
156 $query->select = array($attr);
157 $query->groupby = array();
158
159 $singleton = singleton($this->type);
160 $singleton->extend('augmentSQL', $query);
161
162 return $query;
163 }
164 }
165
[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.
-