/** * Sets the active field value in the Search Index data property according * to the scope of the Searchable Model record * * @param AppModel $model * @param boolean $created */ function _setScope(&$model, $created) { // If the Searchable model doesn't have scope, just go back. if (empty($this->settings[$model->alias]['scope'])) { return; } // Check whether the Searchable Model scope has actually been set, i.e. the // scope data is available in model data. If it is not and the record has // not just been created, do not explicitly set the active field in the // Search Index data property - i.e. active will remain unchanged. $scopeFieldsInModelData = array_intersect_key($this->_modelData[$model->alias], $this->settings[$model->alias]['scope']); if (empty($scopeFieldsInModelData) && !$created) { return; } // Find out the scope of the current record in the Searchable Model by // checking whether it meets the scope conditions $conditions = $this->settings[$model->alias]['scope'] + array($model->primaryKey => $this->_foreignKey); $inScope = $model->hasAny($conditions); $this->SearchIndex->data['SearchIndex']['active'] = (int) $inScope; }
/** * Custom validation for scoped pseudo unique model fields * * @param AppModel $Model * @param array $data * @param string $field * @return boolean * @access public */ public function isUniqueInScope($Model, $data, $field) { if (empty($data[$field])) { trigger_error('SluggableBehavior: missing data for VALIDATING unique scoped field ' . $field . ' in model ' . $Model->name, E_USER_ERROR); return false; } $conditions = array($Model->alias . '.' . $field => $data[$field]); if ($Model->id) { $conditions[$Model->alias . '.' . $Model->primaryKey] = '!= ' . $Model->id; } $scope = $this->settings[$Model->alias]['scope']; if (empty($Model->data[$Model->alias][$scope])) { trigger_error('SluggableBehavior: missing data for VALIDATING over scoped field ' . $scope . ' in model ' . $Model->name, E_USER_ERROR); return false; } $conditions[$Model->alias . '.' . $scope] = $Model->data[$Model->alias][$scope]; return !$Model->hasAny($conditions); }