@param $params
@param \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder

@return mixed
515527
public function createQuery($params, $queryBuilder)
    {
        if (!empty($params['text'])) {
            $queryBuilder->text($params['text']);
        }
        $queryBuilder->field('isDraft')->equals(false);

        if (isset($params['sort'])) {
            foreach (explode(",", $params['sort']) as $sort) {
                $queryBuilder->sort($this->filterSort($sort));
            }
        }

        return $queryBuilder;
    }
410721
public function create(ClientBuilder $input, array $queryParams = [])
    {
        $response = $this->api->oauth2()->clients()->create($input->toArray(), $queryParams);

        return new ClientResponse($response);
    }
1110078
public function create(CallbackBuilder $input, array $queryParams = [])
    {
        $response = $this->api->callbacks()->create($input->toArray(), $queryParams);

        return new CallbackResponse($response);
    }
528542
public function setQueryBuilder(QueryBuilder $queryBuilder, $alias)
    {
        $this->queryBuilder = $queryBuilder;
        $this->alias = $alias;

        return $this;
    }
118601
protected function applyResourceOptions(Builder $queryBuilder, array $options = [])
    {
        if (empty($options)) {
            return $queryBuilder;
        }

        extract($options);

        if (isset($includes)) {
            if (!is_array($includes)) {
                throw new InvalidArgumentException('Includes should be an array.');
            }

            $queryBuilder->with($includes);
        }

        if (isset($filter_groups)) {
            $filterJoins = $this->applyFilterGroups($queryBuilder, $filter_groups);
        }

        if (isset($sort)) {
            if (!is_array($sort)) {
                throw new InvalidArgumentException('Sort should be an array.');
            }

            if (!isset($filterJoins)) {
                $filterJoins = [];
            }

            $sortingJoins = $this->applySorting($queryBuilder, $sort, $filterJoins);
        }

        if (isset($limit)) {
            $queryBuilder->limit($limit);
        }

        if (isset($page)) {
            if (! isset($limit)) {
                throw new InvalidArgumentException('A limit is required when using page.');
            }

            $queryBuilder->offset($page * $limit);
        }

        if (isset($distinct)) {
            $queryBuilder->distinct();
        }

        return $queryBuilder;
    }
566543
private function getQuerySelectQuery(): QueryBuilder
    {
        $query = $this->connection->createQueryBuilder();
        $query->select('DISTINCT q.*, qt.*')
            ->from('ngbm_collection_query', 'q')
            ->innerJoin(
                'q',
                'ngbm_collection_query_translation',
                'qt',
                $query->expr()->andX(
                    $query->expr()->eq('qt.query_id', 'q.id'),
                    $query->expr()->eq('qt.status', 'q.status')
                )
            );

        return $query;
    }
1347505
protected function buildQuery(array $queryParams = [])
    {
        $auth = [
            'appId' => $this->config['appId'],
            'appKey' => $this->config['appKey'],
        ];

        $queryParams = array_merge($auth, $queryParams);

        if (isset($queryParams['extendedOptions'])) {
            $extendedOptions = $queryParams['extendedOptions'];
        } else {
            $extendedOptions = [];
        }

        if (!is_array($extendedOptions)) {
            $extendedOptions = [$extendedOptions];
        }

        if ($this->config['use_http_errors'] &&
            !in_array('useHTTPErrors', $extendedOptions)
        ) {
            $extendedOptions[] = 'useHTTPErrors';
        }

        $queryParams['extendedOptions'] = implode('+', $extendedOptions);

        return $queryParams;
    }
190207
public function buildQuery(QueryBuilder $qb, array $parameters)
    {

        //Handle single tag
        if (!is_array($parameters['tags'])) {
            $parameters['tags'] = [$parameters['tags']];
        }

        //clean the parameters from the blank value
        foreach ($parameters['tags'] as $index => $parameter) {
            //the blank value is removed
            if ($parameter === '') {
                unset($parameters['tags'][$index]);
            }
        }

        if (count($parameters['tags']) > 0) {
            if (array_key_exists('strict', $parameters)) {
                $repository = $this->getEntityManager()->getRepository('VictoireBlogBundle:Article');
                foreach ($parameters['tags'] as $index => $tag) {
                    $parameter = ':tag'.$index;
                    $subquery = $repository->createQueryBuilder('article_'.$index)
                                ->join('article_'.$index.'.tags', 'tag_'.$index)
                                ->where('tag_'.$index.' = '.$parameter);
                    $qb->andWhere($qb->expr()->in('main_item', $subquery->getDql()))
                                ->setParameter($parameter, $tag);
                }
            } else {
                $qb = $qb
                        ->join('main_item.tags', 't')
                        ->andWhere('t.id IN (:tags)')
                        ->setParameter('tags', $parameters['tags']);
            }
        }

        return $qb;
    }
258287
protected function getQueryBuilderForDemand(): QueryBuilder
    {
        $queryBuilder = $this->getQueryBuilder();
        $queryBuilder
            ->select('*')
            ->from('sys_redirect')
            ->orderBy('source_host')
            ->addOrderBy('source_path');

        $constraints = [];
        if ($this->demand->hasSourceHost()) {
            $constraints[] =$queryBuilder->expr()->eq(
                'source_host',
                $queryBuilder->createNamedParameter($this->demand->getSourceHost(), \PDO::PARAM_STR)
            );
        }

        if ($this->demand->hasSourcePath()) {
            $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getSourcePath()) . '%';
            $constraints[] = $queryBuilder->expr()->like(
                'source_path',
                $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
            );
        }

        if ($this->demand->hasTarget()) {
            $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($this->demand->getTarget()) . '%';
            $constraints[] = $queryBuilder->expr()->like(
                'target',
                $queryBuilder->createNamedParameter($escapedLikeString, \PDO::PARAM_STR)
            );
        }

        if ($this->demand->hasStatusCode()) {
            $constraints[] =$queryBuilder->expr()->eq(
                'target_statuscode',
                $queryBuilder->createNamedParameter($this->demand->getStatusCode(), \PDO::PARAM_INT)
            );
        }

        if (!empty($constraints)) {
            $queryBuilder->where(...$constraints);
        }
        return $queryBuilder;
    }
390478
public function createQuery($params, $queryBuilder)
    {
        $userID = $this->auth->getUser()->getId();
        if ($params instanceof Parameters) {
            $value = $params->toArray();
        } else {
            $value = $params;
        }
    
        if (isset($value['job']) && !empty($value['job'])) {
            $queryBuilder->field('job')->equals($value['job']);
        }

        if (isset($value['unread']) && !empty($value['unread'])) {
            $queryBuilder->field('readBy')->notEqual($userID);
        }

        if (isset($value['q']) && !empty($value['q'])) {
            $search = strtolower($value['q']);
            $searchPatterns = array();
    
            foreach (explode(' ', $search) as $searchItem) {
                $searchPatterns[] = new \MongoRegex('/^' . $searchItem . '/');
            }
            $queryBuilder->field('keywords')->all($searchPatterns);
        }
        
        /*
         * We only show applications to which the user has view permissions.
         * and which are not in draft mode
         */
        $queryBuilder->field('permissions.view')->equals($userID)
                     ->field('isDraft')->equals(false);

        if (!isset($value['sort'])) {
            $value['sort'] = '-date';
        }
        
        if (isset($value['status']) && 'all' != $value['status']) {
            $queryBuilder->field('status.name')->equals($value['status']);
        }
        $queryBuilder->sort($this->filterSort($value['sort']));
        
        return $queryBuilder;
    }
686285
public function setQueryBuilder(\pwf\components\querybuilder\interfaces\SelectBuilder $builder)
    {
        $this->queryBuilder = $builder;
        return $this;
    }
646152
public function adaptQueryBuilder(QueryBuilder $queryBuilder, array $params = array())
    {
        parent::adaptQueryBuilder($queryBuilder);
        $queryBuilder
                ->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id')
                ->andWhere('n.id = :node')
                ->andWhere('b.lang = :lang')
                ->setParameter('node', $this->nodeTranslation->getNode()->getId())
                ->setParameter('lang', $this->nodeTranslation->getLang())
                ->addOrderBy('b.created', 'DESC');
    }
129917
private function removeDuplicateJoins(QueryBuilder $queryBuilder)
    {
        if (empty($queryBuilder->getDQLParts()['join'])) {
            return $queryBuilder;
        }
        $joinArray = [];
        $joins = $queryBuilder->getDQLParts()['join']['a'];
        foreach ($joins as $join) {
            $joinArray[] = $join->getAlias();
        }
        $joinArray = array_unique($joinArray);
        foreach ($joins as $key => $join) {
            if (!array_key_exists($key, $joinArray)) {
                unset($joins[$key]);
            }
        }

        $queryBuilder->resetDQLPart('join');
        foreach ($joins as $join) {
            $queryBuilder->join($join->getJoin(), $join->getAlias());
        }

        return $queryBuilder;
    }
114925
public function paginate(Paginator $paginator, $params = [])
    {
        $query = $this->paramsToQuery($params);

        $countQuery = $this->getDB()->newSelect();
        $countQuery->count(['*', 'count']);
        $countQuery->from([$query, 'tbl']);
        $results = $countQuery->execute()->fetchResults();
        $count = $results[0]['count'];

        $paginator->setCount($count);

        $params['limit'] = $paginator->getLimits();

        return $this->findByParams($params);
    }
365707
protected function filterOnlines(QueryBuilder $query_builder = null) {

        if (!$query_builder) {
            $query_builder = $this->getBaseBuilder();
        }

        $query_builder->andWhere("e.status = 1");
        return $query_builder;
    }
1184454
protected function getFixedQueryBuilder(QueryBuilder $queryBuilder)
    {
        $queryBuilderId = clone $queryBuilder;
        $rootAlias = current($queryBuilderId->getRootAliases());

        // step 1 : retrieve the targeted class
        $from = $queryBuilderId->getDQLPart('from');
        $class = $from[0]->getFrom();
        $metadata = $queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class);

        // step 2 : retrieve identifier columns
        $idNames = $metadata->getIdentifierFieldNames();

        // step 3 : retrieve the different subjects ids
        $selects = array();
        $idxSelect = '';
        foreach ($idNames as $idName) {
            $select = sprintf('%s.%s', $rootAlias, $idName);
            // Put the ID select on this array to use it on results QB
            $selects[$idName] = $select;
            // Use IDENTITY if id is a relation too. See: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
            // Should work only with doctrine/orm: ~2.2
            $idSelect = $select;
            if ($metadata->hasAssociation($idName)) {
                $idSelect = sprintf('IDENTITY(%s) as %s', $idSelect, $idName);
            }
            $idxSelect .= ($idxSelect !== '' ? ', ' : '') . $idSelect;
        }

        foreach ($queryBuilderId->getDqlParts()['orderBy'] as $sort) {
            $field = chop($sort->getParts()[0], 'ASC');
            $field = chop($field, 'DESC');
            $idxSelect .= ', (' . $field . ')';
        }

        $queryBuilderId->resetDQLPart('select');
        $queryBuilderId->add('select', 'DISTINCT ' . $idxSelect);

        // for SELECT DISTINCT, ORDER BY expressions must appear in idxSelect list
        /* Consider
            SELECT DISTINCT x FROM tab ORDER BY y;
        For any particular x-value in the table there might be many different y
        values.  Which one will you use to sort that x-value in the output?
        */
        // todo : check how doctrine behave, potential SQL injection here ...
        if ($this->getSortBy()) {
            $sortBy = $this->getSortBy();
            if (strpos($sortBy, '.') === false) { // add the current alias
                $sortBy = $rootAlias . '.' . $sortBy;
            }
            $sortBy .= ' AS __order_by';
            $queryBuilderId->addSelect($sortBy);
        }

        // Use ILIKE instead of LIKE for Postgresql
        if ('pdo_pgsql' == $queryBuilderId->getEntityManager()->getConnection()->getDriver()->getName()) {
            $this->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Blast\DoctrinePgsqlBundle\DoctrineExtensions\BlastWalker');
        }

        $query = $queryBuilderId->getQuery();
        foreach ($this->_hints as $name => $value) {
            $query->setHint($name, $value);
        }

        $results = $query->execute(array(), Query::HYDRATE_ARRAY);
        $platform = $queryBuilderId->getEntityManager()->getConnection()->getDatabasePlatform();
        $idxMatrix = array();
        foreach ($results as $id) {
            foreach ($idNames as $idName) {
                // Convert ids to database value in case of custom type, if provided.
                $fieldType = $metadata->getTypeOfField($idName);
                $idxMatrix[$idName][] = $fieldType && Type::hasType($fieldType)
                    ? Type::getType($fieldType)->convertToDatabaseValue($id[$idName], $platform)
                    : $id[$idName];
            }
        }

        // step 4 : alter the query to match the targeted ids
        foreach ($idxMatrix as $idName => $idx) {
            if (count($idx) > 0) {
                $idxParamName = sprintf('%s_idx', $idName);
                $idxParamName = preg_replace('/[^\w]+/', '_', $idxParamName);
                $queryBuilder->andWhere(sprintf('%s IN (:%s)', $selects[$idName], $idxParamName));
                $queryBuilder->setParameter($idxParamName, $idx);
                $queryBuilder->setMaxResults(null);
                $queryBuilder->setFirstResult(null);
            }
        }

        return $queryBuilder;
    }
178225
public function getQuery(QueryBuilder $queryBuilder)
    {
        $action = $this->getRule()->getAction();
        $foundType = preg_match('/\[\[(.*)\]\]/', $action, $matches);

        if ($foundType) {
            $type = $matches[1];
            $parts = explode(']]', $action);
            $action = ($foundType) ? $parts[1] : $action;

            return $queryBuilder
                ->join('l.resourceType', 'rt')
                ->andWhere('l.action = :action')
                ->andWhere('rt.name = :type')
                ->setParameter('action', $action)
                ->setParameter('type', $type);
        } else {
            return $queryBuilder
                ->andWhere('l.action = :action')
                ->setParameter('action', $action);
        }
    }