function updateCounterCache($keys = array(), $created = false) { parent::updateCounterCache($keys); if (isset($this->data['Purchase'])) { $this->log('Purchase::updateCounterCache'); $this->log(array_keys($this->data)); $cat_id = $this->data['Purchase']['category_id']; $date = $this->data['Purchase']['date']; // UPDATE MONTH TOTALS $dts = strtotime($date); $month = date('Y-m-01', $dts); $dts = strtotime($month); $next_month = date('Y-m-01', strtotime('+1 month', $dts)); $month_sum = $this->find('first', array('fields' => 'SUM(amount) as amount', 'conditions' => array("Purchase.date >= '{$month}'", "Purchase.date < '{$next_month}'", 'Purchase.category_id' => $cat_id))); $month_sum = Set::extract($month_sum, 'Purchase.amount'); App::Import('Model', 'MonthTotal'); $m = new MonthTotal(); $data = array('MonthTotal' => array('id' => $month . '-' . $cat_id, 'date' => $month, 'category_id' => $cat_id, 'amount' => $month_sum)); $m->save($data); // UPDATE YEAR TOTALS $year = date('Y', strtotime($date)) . '-01-01'; $dts = strtotime($date); $year = date('Y-01-01', $dts); $dts = strtotime($year); $next_year = date('Y-01-01', strtotime('+1 year', $dts)); $year_sum = $this->find('first', array('fields' => 'SUM(amount) as amount', 'conditions' => array("Purchase.date >= '{$year}'", "Purchase.date < '{$next_year}'", 'Purchase.category_id' => $cat_id))); $year_sum = Set::extract($year_sum, 'Purchase.amount'); App::Import('Model', 'YearTotal'); $m = new YearTotal(); $data = array('YearTotal' => array('id' => $year . '-' . $cat_id, 'date' => $year, 'category_id' => $cat_id, 'amount' => $year_sum)); $m->save($data); } }
/** * Counter cache should not happen when you are in ElasticSearch mode * * Updates the counter cache of belongsTo associations after a save or delete operation * * 'counterScope' defined get updated * * @param array $keys (optional) Optional foreign key data, defaults to the information $this->data * @param boolean $created (optional) True if a new record was created, otherwise only associations with * @return void */ public function updateCounterCache($keys = array(), $created = false) { if ($this->isElastic()) { return; } return parent::updateCounterCache($keys, $created); }