beforeFind Callback

@param Model $Model Model find is being run on.
@param array $query Array of Query parameters.
@return array Modified query
193391
public function beforeFind(Model $Model, $query) {
		$this->settings[$Model->alias] = $this->modelDefaults[$Model->alias];

		// check if $this->modelDefaultsPersist has been set
		if (isset($this->modelDefaultsPersist[$Model->alias])) {
			// if persist equals equals true
			if (!isset($this->modelDefaults[$Model->alias]['persist']) || $this->modelDefaults[$Model->alias]['persist'] == false) {
				$this->modelDefaults[$Model->alias] = $this->modelDefaultsPersist[$Model->alias];
			}
		}

		if (isset($query['permit']) && isset($this->settings[$Model->alias]['rules'][$query['permit']])) {
			$rules = $this->settings[$Model->alias]['rules'][$query['permit']];
			if (isset($rules['rules'])) {
				unset($rules['rules']);
			}

			$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $rules);
		}

		foreach (array('message', 'check', 'value', 'field', 'skip', 'rules') as $key) {
			if (isset($query['permit_' . $key])) {
				$this->settings[$Model->alias][$key] = $query['permit_' . $key];
			}
		}

		return $query;
	}
326523
public function beforeSave(Model $model, $options = array()) {
		$fields = (array)$this->__settings[$model->alias]['fields'];
		$scope = (array)$this->__settings[$model->alias]['scope'];
		$conditions = !empty($this->__settings[$model->alias]['conditions']) ? (array)$this->__settings[$model->alias]['conditions'] : array();
		$slugfield = $this->__settings[$model->alias]['slugfield'];
		$hasFields = true;

		foreach ($fields as $field) {
			if (!$model->hasField($field)) {
				$hasFields = false;
			}

			if (!isset($model->data[$model->alias][$field])) {
				$hasFields = false;
			}
		}

		if ($hasFields && $model->hasField($slugfield) && ($this->__settings[$model->alias]['overwrite'] || empty($model->id))) {
			$toSlug = array();

			foreach ($fields as $field) {
				$toSlug[] = $model->data[$model->alias][$field];
			}

			$toSlug = join(' ', $toSlug);

			$slug = Inflector::slug($toSlug, $this->__settings[$model->alias]['separator']);

			if ($this->__settings[$model->alias]['lower']) {
				$slug = strtolower($slug);
			}

			if (strlen($slug) > $this->__settings[$model->alias]['length']) {
				$slug = substr($slug, 0, $this->__settings[$model->alias]['length']);
			}

			$conditions[$model->alias . '.' . $slugfield . ' LIKE'] = $slug . '%';

			if (!empty($model->id)) {
				$conditions[$model->alias . '.' . $model->primaryKey . ' !='] = $model->id;
			}

			if (!empty($scope)) {
				foreach ($scope as $s) {
					if (isset($model->data[$model->alias][$s])) {
						$conditions[$model->alias . '.' . $s] = $model->data[$model->alias][$s];
					}
				}
			}

			$sameUrls = $model->find('all', array(
				'recursive' => -1,
				'conditions' => $conditions
			));

			$sameUrls = (!empty($sameUrls))
				? Hash::extract($sameUrls, '{n}.' . $model->alias . '.' . $slugfield)
				: array();

			if (!empty($sameUrls)) {
				if (in_array($slug, $sameUrls)) {
					$begginingSlug = $slug;
					$index = 1;

					while ($index > 0) {
						if (!in_array($begginingSlug . $this->__settings[$model->alias]['separator'] . $index, $sameUrls)) {
							$slug = $begginingSlug . $this->__settings[$model->alias]['separator'] . $index;
							$index = -1;
						}

						$index++;
					}
				}
			}

			$model->data[$model->alias][$slugfield] = $slug;
		}

		return parent::beforeSave($model);
	}
1200030
public function moveBefore($model)
    {
        if ( is_numeric($model) ) {
            $model = call_user_func("$this->ownerClassName::findOne", $model);
        }
        $parentId = $this->parentIdField ? $model->{$this->parentIdField} : 0;
        if ( $model->{$this->sortField} < 1 ) {
            $this->reorder($parentId);
            $model->refresh();
        }
        $prevIdx = $this->getMaxIdx($parentId, $model->{$this->sortField});
        if ( $model->{$this->sortField} - $prevIdx < 2 ) {
            $this->reorder($parentId);
            $model->refresh();
            $prevIdx = $this->getMaxIdx($parentId, $model->{$this->sortField});
        }
        $newIdx = $model->{$this->sortField} - floor(($model->{$this->sortField} - $prevIdx)/2);
        if ( $this->parentIdField ) {
            call_user_func("$this->ownerClassName::updateAll", [
                $this->sortField => $newIdx,
                $this->parentIdField => $parentId,
            ], [ $this->primaryKey => $this->owner->primaryKey ]);
        } else {
            call_user_func("$this->ownerClassName::updateAll",
                [ $this->sortField => $newIdx ],
                [ $this->primaryKey => $this->owner->primaryKey ]);
        }
        $this->invalidateTagDependency();
    }
719215
protected function getModelInstanceQuery(Model $model)
    {
        $query = $model->query();

        $this->applyOrderingToModelInstanceQuery($query);
        $this->applyStartAndStopLimitsToQuery($query);

        return $query;
    }
36094
public function	beforeFind(Event $event, Query $query, ArrayObject $options, $primary) 
    {
        if ($primary === false) {
            return $query;
        }
        
        if ($this->hasField('user_id') && \Rita::$user['role_id'] !== 1) {
            $query->where(['user_id' => \Rita::$user['id']]);    
        }    
        
        return $query;
    }
386124
public function afterFind(Model $model, $results, $primary = true) {
        $alias = $model->alias;

        if (!$this->format || ($model->id && !$this->onUpdate) || empty($this->_enums[$alias])) {
            return $results;
        }

        if ($results) {
            $enum = $this->_enums[$alias];

            foreach ($results as &$result) {
                foreach ($enum as $key => $nop) {
                    if (isset($result[$alias][$key])) {
                        $value = $result[$alias][$key];

                        if ($this->format === self::REPLACE) {
                            $result[$alias][$key] = $this->enum($model, $key, $value);

                            if ($this->persist) {
                                $result[$alias][$key . $this->suffix] = $value;
                            }
                        } else if ($this->format === self::APPEND) {
                            $result[$alias][$key . $this->suffix] = $this->enum($model, $key, $value);
                        }
                    }
                }
            }
        }

        return $results;
    }
1193662
public function find($query)
    {
        // parse query
        $query = static::clean($query);
        list($method, $uri) = explode(' ', $query);

        // search in all routes
        foreach($this->routes as $route) {
            $route->compiled = static::compile($route->uri);
            if(preg_match($route->compiled, $uri, $params) and ($route->method == 'ANY' or $method == $route->method)) {
                $route->params = array_filter($params, 'is_string', ARRAY_FILTER_USE_KEY);
                return $route;
            }
        }
    }
778669
protected static function applySortableGroup($query, $model)
    {
        $sortableGroupField = static::getSortableGroupField();

        if (is_array($sortableGroupField)) {
            foreach ($sortableGroupField as $field) {
                $query = $query->where($field, $model->$field);
            }
        } elseif ($sortableGroupField !== null) {
            $query = $query->where($sortableGroupField, $model->$sortableGroupField);
        }

        return $query;
    }
228500
protected function search(ModelCriteria $query, Request $request)
    {
        $filters = $this->getFilters($request);

        if (empty($filters)) {
            return $query;
        }

        $conditions = array();

        $columns = $this->getSearchColumns($request);

        if (is_array($filters)) {
            foreach ($columns as $name => $condition) {
                $items = explode('.', $name);
                $i = 0;
                $values = $filters;

                while (is_array($values) && array_key_exists($items[$i], $values)) {
                    $values = $values[$items[$i]];
                    $i++;
                }

                if (is_array($values)) {
                    continue;
                } else {
                    $value = trim($values);
                }

                if (empty($value) && !is_numeric($value)) {
                    continue;
                }

                $query->condition(
                    'search_'.$name,
                    sprintf($condition, $value)
                );

                $conditions[] = 'search_'.$name;
            }

            if (!empty($conditions)) {
                return $query->where($conditions, 'and');
            }
        } else {
            $value = trim($filters);

            foreach ($columns as $name => $condition) {
                $query->condition(
                    'search_'.$name,
                    sprintf($condition, $value)
                );

                $conditions[] = 'search_'.$name;
            }

            if (!empty($conditions)) {
                return $query->where($conditions, 'or');
            }
        }
    }
510540
protected function processMultipleResults($query)
    {
        if (!empty($this->fetchRefs)) {
            $models = [];
            foreach ($query->fetchAll() as $model) {
                $models[] = $this->populateModelFromJoin($model);
            }
        } else {
            $models = $query->fetchAllClass($this->modelClass);
        }
        if (isset($this->manager)) {
            foreach ($models as $model) {
                $this->manager->manageModel($model);
            }
        }
        $this->fetchReferences(false);
        return $models;
    }
321126
protected function doFindOne(array $query, array $fields)
    {
        $riakCollection = $this->riakCollection;
        return $this->retry(function() use ($riakCollection, $query, $fields) {
            return $riakCollection->findOne($query, $fields);
        });
    }
799538
protected function _doFind(Model $model, array $where, $type = 'first') {
        $virtual = $model->virtualFields;
        $model->virtualFields = array();

        $results = $model->find($type, array(
            'conditions' => $where,
            'contain' => false,
            'recursive' => -1,
            'order' => ''
        ));

        $model->virtualFields = $virtual;

        return $results;
    }
660962
public function afterFind(Model $Model, $results, $primary) {
		if (!$primary) {
			return $results;
		}

		$settings = $this->settings[$Model->alias];

		if ($settings['skip'] === true) {
			return $results;
		}

		// the permit behavour is a bit pointless if we're handing more than one result
		if (count($results) > 1) {
			return $results;
		}

		// HACK: Retrieve the zeroth index in the resultset
		$userId = Hash::get($results, "0.{$settings['field']}");
		if ($userId === null) {
			return $results;
		}

		if ($userId == $this->user($Model, $results, 'id')) {
			return $results;
		}

		$adminCheck = $settings['check'];
		$adminValue = $settings['value'];
		if ($adminCheck && $this->user($Model, $results, $adminCheck) == $adminValue) {
			return $results;
		}

		throw new UnauthorizedException($settings['message']);
	}
637240
public function scopeOrderModel($query)
    {
        $parentFields = $this->getParentFieldName();
        if ($parentFields) {
            $parentFields = (array) $parentFields;
            foreach ($parentFields as $parentFieldName) {
                $query->where($parentFieldName, $this->{$parentFieldName});
            }
        }

        return $query->orderBy($this->getOrderField(), 'ASC');
    }
1282890
protected function doFind(array $query, array $fields)
    {
        $riakCollection = $this->riakCollection;
        $cursor = $this->retry(function() use ($riakCollection, $query, $fields) {
            return $riakCollection->find($query, $fields);
        });
        return $this->wrapCursor($cursor, $query, $fields);
    }
1067286
public function searchTypeAhead(Model $model, array $query) {
        if ($model->hasMethod('searchTypeAhead')) {
            return $model->searchTypeAhead($query);
        }

        $keyword = $query['term'];
        unset($query['term']);

        $results = $model->find('all', array(
            'conditions' => array($model->alias . '.' . $model->displayField . ' LIKE' => '%' . $keyword . '%') + $query,
            'order' => array($model->alias . '.' . $model->displayField => 'ASC'),
            'contain' => false
        ));
        $data = array();

        foreach ($results as $result) {
            $data[] = array(
                'id' => $result[$model->alias][$model->primaryKey],
                'title' => $result[$model->alias][$model->displayField]
            );
        }

        return $data;
    }
1024171
public function findOne($query = [], $fields = [])
    {
        if (is_string($query)) {
            return $this->collection->findOne($query, $fields = []);
        }

        return parent::findOne($query, $fields);
    }