/** * Identifies the ids of the nodes whose counter caches need updating. * - If editing and parent not changed, do not update counter caches * - If inserting and parent id is null, do not update counter caches * - If editing and parent has changed, update counter caches of old parent * and all its parents and new parent and all it's parents. * - If inserting and parent is not null, update counter caches of new parent * and all it's parents. * * @param AppModel $model The model object that the behavior is attached to * @return boolean Always true * @access public */ function beforeSave(&$model) { $this->_parentIds[$model->alias] = array(); // Let TreeBehavior do it's thing first $parentResult = parent::beforeSave($model); // If parent fails, return if ($parentResult === false) { return $parentResult; } // If editing and parent not changed, do not update counter caches if ($model->id && !$this->settings[$model->alias]['__parentChange']) { return true; } // Get the new parent id $newParentId = null; if (isset($model->data[$model->alias][$this->settings[$model->alias]['parent']])) { $newParentId = $model->data[$model->alias][$this->settings[$model->alias]['parent']]; } // If inserting and new parent id is null, do not update counter caches if (!$model->id && !$newParentId) { return true; } // If editing get ids of the current/previous parent and all it's parents if ($model->id) { // Get the current/previous parent id $currentParentId = $model->field($this->settings[$model->alias]['parent']); // Add the current/previous parent id and it's parents to the _parentIds // property for updating in afterSave $this->_addParentIds($model, $currentParentId); } // Add the new parent id and it's parents to the _parentIds property for // updating in afterSave $this->_addParentIds($model, $newParentId); return true; }
public function beforeSave($Model) { if ($this->scoped($Model)) { if (!$Model->id || $Model->id && array_key_exists($this->settings[$Model->alias]['parent'], $Model->data)) { if (!$this->__setScope($Model, $Model->data[$Model->alias], true)) { return false; } } } if ($this->counterCacheEnabled($Model) && $Model->id) { $this->__oldParentId = $Model->field($this->settings[$Model->alias]['parent']); } return parent::beforeSave($Model); }