@param Builder $query
@param string  $title
@param int     $limit

@return Builder
775280
public function scopeAutocompleteTitle(Builder $query, $title, $limit)
    {
        return $query
            ->currentVersion()
            ->select('title', 'primary_uri')
            ->where('title', 'like', '%'.$title.'%')
            ->limit($limit)
            ->orderBy(DB::raw('length(title)'), 'asc');
    }
398739
public function scopeAdministrators(Builder $query)
    {
        $query->with('roles')->whereNotNull('tbl_users.id')->whereHas('roles', function ($query) {
            $query->whereNotIn('tbl_roles.name', ['member', 'guest', 'client']);
        });


        return $query;
    }
716708
protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();

        if (!is_array($keys)) {
            return parent::setKeysForSaveQuery($query);
        }

        foreach ($keys as $key) {
            $query->where($key, '=', $this->getKeyForSaveQuery($key));
        }

        return $query;
    }
1313429
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('discussion_id', $this->discussion_id)
              ->where('user_id', $this->user_id);

        return $query;
    }
625515
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('tag_id', $this->tag_id)
              ->where('user_id', $this->user_id);

        return $query;
    }
340914
protected function limit(&$query)
    {
        switch ($this->mode) {

            case "mysql":
            case "postgres":
            case "redshift":
            case "sqlite":
                $query = preg_replace("/\bFETCH\s+FIRST\s+([0-9]+)\s+ROW(S?)\s+ONLY\b/i", "\nLIMIT $1\n", $query);
                break;

            case "odbc":
                $query = preg_replace("/\bLIMIT\s+([0-9]+)\b/i", "\nFETCH FIRST $1 ROWS ONLY\n", $query);
                break;
        }
    }
1168909
public function search(string $query, $limit = 30, $start = 0, $sort = null)
    {
        if ($query == '*.*' && $limit == 30 && $start == 0 && $sort == 'lastupdated desc') {
            $result = $this->fetchFromCache();

            if (null === $result) {
                $result = $this->search->search($query, $limit, $start, $sort);
                $this->saveToCache($result);
            }

            return $result;
        } else {
            return $this->search->search($query, $limit, $start, $sort);
        }
    }
795052
public function applyToQuery(Builder $query)
    {
        $this->query = $query;

        foreach ($this->request->all() as $filter => $value) {
            $method = Str::camel($filter);

            if ($this->filterCanBeApplied($method, $value)) {
                call_user_func([$this, $method], $value);
            }
        }

        return $query;
    }
619917
private function getSearchQueryLimit($query, $limit, $offset)
	{
		if ($limit > -1) {
			$query->limit($limit);
			$query->offset($offset);
		}

		return $query;
	}
774601
protected function setKeysForSaveQuery(Builder $query)
	{
		$query->where($this->morphType, $this->getAttribute($this->morphType));

		return parent::setKeysForSaveQuery($query);
	}
929709
protected function search(Builder $query, Request $request): Builder
    {
        $phrase = $request->input('q');

        if (!$phrase || strlen($phrase) < $this->minimumSearchInputLength) {
            return $query;
        }

        if ($this->translationModel()) {
            return $this->searchQueryTranslations($query, $phrase);
        }

        return $this->searchQuery($query, $phrase);
    }
886198
public function pagination($page, $limit, $query = null, $orderBy = null, $sort = 'ASC', $filter = null)
    {
        $entityClassAlias = $this->getEntityClassAlias();

        // Create a Doctrine Collection
        $queryBuilder = $this->getObjectManager()->createQueryBuilder();
        $queryBuilder->select($entityClassAlias)
                     ->from($this->getEntityClass(), $entityClassAlias);

        $paramCounter = 0;

        if (is_array($filter)) {
            $paramCounter = $this->addFilterToQueryBuilder($filter, $queryBuilder);
        }

        //Add joins to query builder
        $this->addJoinTablesToQueryBuilder($queryBuilder);

        if (null !== $query) {
            $properties    = $this->getQueryableProperties();
            $orExpressions = array();
            $paramCounter++;

            foreach ($properties as $property) {
                $orExpressions[] = $queryBuilder->expr()->like($property, '?' . $paramCounter);
            }

            $queryBuilder->andWhere(
                call_user_func_array(array($queryBuilder->expr(), "orX"), $orExpressions)
            );

            $queryBuilder->setParameter($paramCounter, $query);
        }

        if ($orderBy) {
            $orderByIdentifier = $this->getOrderByIdentifier($orderBy);

            if (!$sort) {
                $sort = 'ASC';
            }
            $queryBuilder->orderBy($orderByIdentifier, $sort);
        }

        // Create the paginator itself
        $paginator = new Paginator(
            new DoctrinePaginator(new ORMPaginator($queryBuilder))
        );

        $paginator->setCurrentPageNumber($page)
                  ->setItemCountPerPage($limit)
                  ->setPageRange(5);

        return $paginator;
    }
996815
public function compileUpdate(Builder $query, $values)
    {
        $sql = parent::compileUpdate($query, $values);

        if (isset($query->orders)) {
            $sql .= ' ' .$this->compileOrders($query, $query->orders);
        }

        if (isset($query->limit)) {
            $sql .= ' ' .$this->compileLimit($query, $query->limit);
        }

        return rtrim($sql);
    }
461884
protected function _buildLimitPart($limit, $query)
    {
        if ($limit === null || $query->clause('offset') === null) {
            return '';
        }

        return sprintf(' FETCH FIRST %d ROWS ONLY', $limit);
    }
732978
protected function buildQuery(Builder $query, $name)
    {
        $op = $this->getOperator();
        $value = $this->getValue();

        switch ($op) {
            case 'in':
                $query->whereIn($name, (array) $value);
                break;
            case 'between':
                $query->whereBetween($name, (array) $value);
                break;
            case 'like':
                $value .= '%';
                $query->where($name, $op, $value);
                break;
            default:
                $query->where($name, $op, $value);
        }
    }
798508
public function getAutocomplete($query = null, $target = null, $limit = 0) {
		$result = [];
		if (empty($query)) {
			return $result;
		}

		$query = trim($query);
		if (empty($query)) {
			return $result;
		}

		$limit = (int)$limit;
		if ($limit <= 0) {
			$limit = CAKE_SEARCH_INFO_AUTOCOMPLETE_LIMIT;
		}

		$language = (string)Configure::read('Config.language');
		$dataStr = serialize(compact('query', 'target', 'limit')) . '_' . $language;
		$cachePath = 'QueryResultAutocom.' . md5($dataStr);
		$cached = Cache::read($cachePath, CAKE_SEARCH_INFO_CACHE_KEY_QUERY_RESULT);
		if (!empty($cached)) {
			return $cached;
		}

		$querySearchMinLength = $this->_modelConfigSearchInfo->getQuerySearchMinLength();
		if ($querySearchMinLength < 1) {
			$querySearchMinLength = CAKE_SEARCH_INFO_QUERY_SEARCH_MIN_LENGTH;
		}

		if (mb_strlen($query, 'UTF-8') < $querySearchMinLength) {
			return $result;
		}

		$textConv = new Text_LangCorrect();
		$queryConfig = $this->getQueryConfig($target);
		$lang = (string)Configure::read('Config.language');
		foreach ($queryConfig['modelConfig'] as $modelName => $config) {
			$resultItem = $this->_getAutocompleteData($modelName, $config, $query, $queryConfig['anyPart'], $limit);
			if (empty($resultItem)) {
				if (mb_strtolower($lang) !== 'rus') {
					continue;
				}
				$queryCorrect = $textConv->parse($query, Text_LangCorrect::SIMILAR_CHARS | Text_LangCorrect::KEYBOARD_LAYOUT);
				if ($query === $queryCorrect) {
					continue;
				}

				$resultItem = $this->_getAutocompleteData($modelName, $config, $queryCorrect, $queryConfig['anyPart'], $limit);
				if (empty($resultItem)) {
					continue;
				}
			}

			$result = array_merge($result, $resultItem);
		}

		$truncateOpt = [
			'ellipsis' => '',
			'exact' => false,
			'html' => false
		];
		if (!empty($result)) {
			$result = array_values(array_unique($result));
			foreach ($result as &$resultItem) {
				$resultItem = CakeText::truncate(h($resultItem), CAKE_SEARCH_INFO_AUTOCOMPLETE_RESULT_TRUNCATE_LIMIT, $truncateOpt);
			}
		}
		Cache::write($cachePath, $result, CAKE_SEARCH_INFO_CACHE_KEY_QUERY_RESULT);

		return $result;
	}
893580
public function compile_limit_with_offset( &$query ) 
	{
		if ( is_null( $query->limit ) )
		{
			return "";
		}

		return ' limit '.( (int) $query->offset ).', '.( (int) $query->limit );
	}