Example #1
0
 /**
  * Updates the sum and counter cache of belongsTo associations after a save or delete operation
  *
  * @param array $keys Optional foreign key data, defaults to the information $this->data
  * @param boolean $created True if a new record was created, otherwise only associations with
  *                                  'counterScope' defined get updated
  * @return void
  * @access public
  */
 function updateCounterCache($keys = array(), $created = false)
 {
     parent::updateCounterCache($keys);
     if (empty($keys)) {
         $keys = $this->data[$this->alias];
     }
     foreach ($this->belongsTo as $parent => $assoc) {
         if (!empty($assoc['sumCache'])) {
             if ($assoc['sumCache'] === true) {
                 $assoc['sumCache'] = Inflector::underscore($this->alias) . '_sum';
             }
             if ($this->{$parent}->hasField($assoc['sumCache'])) {
                 $conditions = array($this->escapeField($assoc['foreignKey']) => $keys[$assoc['foreignKey']]);
                 if (isset($assoc['sumScope'])) {
                     $conditions = array_merge($conditions, (array) $assoc['sumScope']);
                 }
                 if (!isset($assoc['sumField'])) {
                     $assoc['sumField'] = 'amount';
                 }
                 $fields = 'SUM(' . $assoc['sumField'] . ') AS ' . $assoc['sumField'] . '';
                 $recursive = -1;
                 list($edge) = array_values($this->find('first', compact('conditions', 'fields', 'recursive')));
                 $sum = ife(empty($edge[$assoc['sumField']]), 0, $edge[$assoc['sumField']]);
                 $this->{$parent}->updateAll(array($assoc['sumCache'] => $sum), array($this->{$parent}->escapeField() => $keys[$assoc['foreignKey']]));
             }
         }
     }
 }
Example #2
0
 function updateCounterCache($keys = array(), $created = false)
 {
     parent::updateCounterCache($keys, $created);
     $this->updateSumCache($keys, $created);
 }
 public function updateCounterCache($keys = array(), $created = false)
 {
     $this->useMasterDb();
     return parent::updateCounterCache($keys, $created);
 }