Before find callback

@param Model $model
@param array $query
@return array
12639
public function beforeFind(Model $model, $query) {

		$runtime = $this->runtime[$model->alias];
		if ($runtime) {
			if (!is_array($query['conditions'])) {
				$query['conditions'] = array();
			}
			$conditions = array_filter(array_keys($query['conditions']));

			$fields = $this->_normalizeFields($model);

			foreach ($fields as $flag => $date) {
				if (true === $runtime || $flag === $runtime) {
					if (!in_array($flag, $conditions) && !in_array($model->name . '.' . $flag, $conditions)) {
						$query['conditions'][$model->alias . '.' . $flag] = false;
					}

					if ($flag === $runtime) {
						break;
					}
				}
			}
			return $query;
		}
	}
910905
public static function make($query, Model $model = null)
    {
        if ($query instanceof EloquentBuilder) {
            $model = $query->getModel();
            $query = $query->getQuery();
        }

        return new Joiner($query, $model);
    }
508113
protected function prepareQuery($query)
    {
        $query = $query->with($this->withs);

        if ($this->withCounts) {
            $query = $query->withCount($this->withCounts);
        }

        if ($this->orderBy) {
            $query = $query->orderBy($this->orderBy, $this->orderByDirection);
        }

        if ( ! empty($this->has)) {
            foreach ($this->has as $has) {
                $query = $query->has($has['relation'], $has['operator'], $has['value']);
            }
        }

        if ( ! empty($this->whereHas)) {
            foreach ($this->whereHas as $has) {
                $query = $query->whereHas($has[0], isset($has[1]) ? $has[1] : null);
            }
        }

        return $query;
    }
689381
protected function formatQuery(Query $query)
    {
        // handle numeric where, from 2 to ['id'=>2]
        if (!$query->formatted() && $where = $query->get('where')) {
            $table = static::getFirstTable($query['table']);
            $atable  = static::getFirstTableAlias($query['table']);
            $model  = static::getQueryModel();
            $pk     = isset($model['primary_key']) ? $model['primary_key'] : null;
            $spk    = $atable . '.' . $pk;
            foreach ($where as $k=>$v) {
                if (is_numeric($k)) {
                    if (is_numeric($v)) {
                        if (!$pk) {
                            throw new Exception('pk is not set:' . $table);
                        }
                        $where[$k] = [$spk => $v];
                    } elseif (is_array($v)) {
                        foreach ($v as $k2=>$v2) {
                            if (is_numeric($k2) && is_numeric($v2)) {
                                if (!$pk) {
                                    throw new Exception('pk is not set:' . $table);
                                }
                                $where[$k][$k2] = [$spk => $v2];
                            }
                        }
                    }
                }
            }
            
            $query->setRaw('where', $where);
        }
        
        return $query;
    }
1030690
public function afterFind(Model $model, $results, $primary=false) {
        $alias = $model->alias;

        foreach ($results as $i => $data) {
            if (empty($data[$alias])) {
                continue;
            }

            foreach ($data[$alias] as $field => $value) {
                if (empty($this->settings[$alias][$field]) || !empty($value)) {
                    continue;
                }

                $attachment = $this->_settingsCallback($model, $this->settings[$alias][$field]);

                if ($attachment['defaultPath']) {
                    $results[$i][$alias][$attachment['dbColumn']] = $attachment['defaultPath'];
                }

                if ($attachment['transforms']) {
                    foreach ($attachment['transforms'] as $transform) {
                        if ($transform['defaultPath']) {
                            $results[$i][$alias][$transform['dbColumn']] = $transform['defaultPath'];
                        }
                    }
                }
            }
        }

        return $results;
    }
631512
public function actionAutoComplete($query)
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $rows = Category::find()->select(['id', 'name'])->where(['like', 'name', $query])->asArray()->all();
        return $rows;
    }
639670
public function exists($model, $query)
    {
        //
        $params = [];

        //
        $whereArray = [];

        //
        if (isset($query['where'])) {
            $whereArray[] = $query['where'];
            unset($query['where']);
        }

        //
        $schema = $this->getFields($model);

        foreach ($schema as $field) {
            if (!isset($query[$field])) {
                continue;
            }
            $value = $query[$field];
            $token = ':'.$field;
            $params[$token] = $value;
            $whereArray[] = "`{$field}` = {$token}";
        }

        $where = count($whereArray) > 0
               ? 'WHERE '.implode(' AND ', $whereArray) : '';

        $table = $this->getPrefix($model);
        $sql = "SELECT * FROM `{$table}` {$where} LIMIT 1";
        $row = $this->getRow($sql, $params);

        return $row;
    }
1123434
public function search($model)
    {
        $method = 'search';

        $condition = $this->condition ?: [[]];

        $params = $this->buildParams('limit', 'offset');

        $result = $this->call($model, $method, $condition, $params);

        //Reset params for future queries.
        $this->resetParams('limit', 'offset', 'condition');

        return $this->makeResponse($result);
    }
850787
public function apply(Builder $query, Model $model)
    {
        Models::scope()->applyToModelQuery($query, $model->getTable());
    }
1130940
protected function getValueFromRelationQuery($query)
    {
        $query = $this->prepareRelationQuery($query);

        return $this->getValueFromModel(
            $query->first()
        );
    }
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;
    }
97341
public function _findComputed(Model $Model, $func, $state, $query, $result = array()) {
		if ('after' == $state) {
			return $result;
		}

		$query['fields'] = array(sprintf(
			'%s(%s) AS %s'
			, strtoupper($this->_methods[strtolower($this->settings[$Model->alias]['method'])])
			, $Model->alias . '.' . $this->settings[$Model->alias]['column']
			, $this->settings[$Model->alias]['result']
		));

		return $query;
	}
30952
public function beforeDelete(Model $model, $cascade = true) {
        if (empty($model->id)) {
            return false;
        }

        return $this->deleteFiles($model, $model->id, array(), true);
    }
94428
protected function _containForeignModels($query, $assoc = null) {
		if (
			(isset($query['contain']) && empty($query['contain']))
			|| (isset($this->contain) && empty($this->contain))
		) {
			return $query;
		}

		if (!isset($query['contain'])) {
			$query['contain'] = array();
		} else if (!is_array($query['contain'])) {
			$query['contain'] = (array) $query['contain'];
		}

		// Add the foreign models.
		foreach (array_keys($this->belongsToForeignModels) as $model) {
			if (!empty($assoc)) {
				$model = "$assoc.$model";
			}
			if (!in_array($model, $query['contain']) && !isset($query['contain'][$model])) {
				$query['contain'][] = $model;
			}
		}

		return $query;
	}
682097
public function afterFind(Model $model, $results, $primary = true) {
        if ($results) {
            foreach ($results as $index => $result) {
                $results[$index] = $this->convert($model, $result, self::FROM);
            }
        }

        return $results;
    }
71695
public static function _fetch_handler( &$query )
	{
		// because the model is an object we force the fetch
		// arguments to obj so that we can still make use of
		// the group by and forward key functions
		$query->fetch_arguments = array( 'obj' );

		// Run the query and assign the reults
		// here we force the fetch arguments to assoc 
		// without this step model::assign will fail
		return static::assign( $query->handler->fetch( $query->build(), $query->handler->builder()->parameters, array( 'assoc' ) ) );
	}
1196607
private function buildQuery(Model $model = null)
    {
        $query = $this->model->collections();

        $query = $query->whereCollectionName($this->collectionName);

        if ($model) {
            $query = $query->where('item_id', $model->id)
                           ->where('item_type', get_class($model));
        }

        if (!$model && $this->itemType) {
            $query = $query->where('item_type', '=', $this->itemType);
        }

        return $query;
    }