Add filter conditions on the condition node instance.

@param FormInterface  $form
@param QueryInterface $filterQuery
@param string         $alias

@throws \RuntimeException
505543
protected function addFilters(FormInterface $form, QueryInterface $filterQuery, $alias = null)
    {
        /** @var $child FormInterface */
        foreach ($form->all() as $child) {
            $formType = $child->getConfig()->getType()->getInnerType();

            // this means we have a relation
            if ($child->getConfig()->hasAttribute('add_shared')) {
                $join = trim($alias . '.' . $child->getName(), '.');

                $addSharedClosure = $child->getConfig()->getAttribute('add_shared');

                if (!$addSharedClosure instanceof \Closure) {
                    throw new \RuntimeException('Please provide a closure to the "add_shared" option.');
                }

                $qbe = new FilterBuilderExecuter($filterQuery, $alias, $this->parts);
                $addSharedClosure($qbe);

                if (!$this->parts->has($join)) {
                    throw new \RuntimeException(sprintf('No alias found for relation "%s".', $join));
                }

                $isCollection = ($formType instanceof CollectionAdapterFilterType);

                $this->addFilters($isCollection ? $child->get(0) : $child, $filterQuery, $this->parts->get($join));

            // Doctrine2 embedded object case
            } elseif ($formType instanceof EmbeddedFilterTypeInterface) {
                $this->addFilters($child, $filterQuery, $alias . '.' . $child->getName());

            // default case
            } else {
                $condition = $this->getFilterCondition($child, $formType, $filterQuery, $alias);

                if ($condition instanceof ConditionInterface) {
                    $this->conditionBuilder->addCondition($condition);
                }
            }
        }
    }
559339
public function updateSearchForm(Form $form)
    {
        /** @var DropdownField $filterField */
        $filterField = $form->Fields()->fieldByName('Search__FilterClass');
        if ($filterField) {
            $filterField->setEmptyString('')->setHasEmptyDefault(false);
        }
    }
565324
protected function filterLimitOffset(&$filter, $select)
    {
        $limit = null;
        $offset = null;

        if (array_key_exists('limit', $filter)) {
            $limit = (int) $filter['limit'];
            unset($filter['limit']);
        }
        if (array_key_exists('offset', $filter)) {
            $offset = (int) $filter['offset'];
            unset($filter['offset']);
        }
        $select->limit($limit, $offset);
    }
32461
public function prepareCondition(&$query, $modelForm)
    {
        foreach ($modelForm->attributes() as $attribute) {
            if ($attribute === 'table' || is_null($modelForm[$attribute])) {
                continue;
            }
            $filterConfig = Yii::$app->controller->module->filters[$attribute];
            $query->andWhere("{$filterConfig['attribute']} {$filterConfig['criteria']['operator']} :{$attribute}", [
                $attribute => $modelForm[$attribute],
            ]);
        }
    }
148423
public function setFilter(FilterQuery $filter)
    {
        $this->result = array_merge($this->result, $filter->toArray()['bool']);

        return $this;
    }
113398
public function filterQuery(&$selects)
    {
        $model      = $this->config->getDataModel();
        $joins      = $where      = '';
        $columnName = $this->getOption('column_name');

        $relationship = $model->{$this->getOption('relationship')}();
        $from_table   = $this->tablePrefix.$model->getTable();
        $field_table  = $columnName.'_'.$from_table;
        $other_table  = $this->tablePrefix.$relationship->getRelated()->getTable();
        $other_alias  = $columnName.'_'.$other_table;
        $other_model  = $relationship->getRelated();
        $other_key    = $other_model->getKeyName();
        $int_table    = $this->tablePrefix.$relationship->getTable();
        $int_alias    = $columnName.'_'.$int_table;
        $column1      = explode('.', $relationship->getForeignKey());
        $column1      = $column1[1];
        $column2      = explode('.', $relationship->getOtherKey());
        $column2      = $column2[1];
        $joins .= ' LEFT JOIN '.$int_table.' AS '.$int_alias.' ON '.$int_alias.'.'.$column1.' = '.$field_table.'.'.$model->getKeyName()
                .' LEFT JOIN '.$other_table.' AS '.$other_alias.' ON '.$other_alias.'.'.$other_key.' = '.$int_alias.'.'.$column2;

        //grab the existing where clauses that the user may have set on the relationship
        $relationshipWheres = $this->getRelationshipWheres($relationship, $other_alias, $int_alias, $int_table);

        $where = $this->tablePrefix.$model->getTable().'.'.$model->getKeyName().' = '.$int_alias.'.'.$column1
                    .($relationshipWheres ? ' AND '.$relationshipWheres : '');

        $selects[] = $this->db->raw('(SELECT '.$this->getOption('select').'
										FROM '.$from_table.' AS '.$field_table.' '.$joins.'
										WHERE '.$where.') AS '.$this->db->getQueryGrammar()->wrap($columnName));
    }
1048588
public function filterQuery(QueryBuilder &$query, &$selects = null)
    {
        //run the parent method
        parent::filterQuery($query, $selects);

        //get the values
        $value   = $this->getOption('value');
        $table   = $this->getOption('table');
        $column  = $this->getOption('column');
        $column2 = $this->getOption('column2');

        //if there is no value, return
        if (!$value) {
            return;
        }

        $model = $this->config->getDataModel();

        //if the table hasn't been joined yet, join it
        if (!$this->validator->isJoined($query, $table)) {
            $query->join($table, $model->getTable().'.'.$model->getKeyName(), '=', $column);
        }

        //add where clause
        $query->whereIn($column2, $value);

        //add having clauses
        $query->havingRaw('COUNT(DISTINCT '.$query->getConnection()->getTablePrefix().$column2.') = '.count($value));

        //add select field
        if ($selects && !in_array($column2, $selects)) {
            $selects[] = $column2;
        }
    }
1312191
protected function filterQueryBuilder(QueryBuilder $queryBuilder, string $field, ListFilter $listFilter)
    {
        $value = $this->filterEasyadminAutocompleteValue($listFilter->getValue());
        // Empty string and numeric keys is considered as "not applied filter"
        if (null === $value || '' === $value || \is_int($field)) {
            return;
        }

        // Add root entity alias if none provided
        $queryField = $listFilter->getProperty();
        if (false === \strpos($queryField, '.')) {
            $queryField = $queryBuilder->getRootAlias().'.'.$queryField;
        }

        // Checks if filter is directly appliable on queryBuilder
        if (!$this->isFilterAppliable($queryBuilder, $queryField)) {
            return;
        }

        $operator = $listFilter->getOperator();
        // Sanitize parameter name
        $parameter = 'form_filter_'.\str_replace('.', '_', $field);

        switch ($operator) {
            case ListFilter::OPERATOR_EQUALS:
                if ('_NULL' === $value) {
                    $queryBuilder->andWhere(\sprintf('%s IS NULL', $queryField));
                } elseif ('_NOT_NULL' === $value) {
                    $queryBuilder->andWhere(\sprintf('%s IS NOT NULL', $queryField));
                } else {
                    $queryBuilder
                        ->andWhere(\sprintf('%s %s :%s', $queryField, '=', $parameter))
                        ->setParameter($parameter, $value)
                    ;
                }
                break;
            case ListFilter::OPERATOR_NOT:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, '!=', $parameter))
                    ->setParameter($parameter, $value)
                ;
                break;
            case ListFilter::OPERATOR_IN:
                // Checks that $value is not an empty Traversable
                if (0 < \count($value)) {
                    $queryBuilder
                        ->andWhere(\sprintf('%s %s (:%s)', $queryField, 'IN', $parameter))
                        ->setParameter($parameter, $value)
                    ;
                }
                break;
            case ListFilter::OPERATOR_NOTIN:
                // Checks that $value is not an empty Traversable
                if (0 < \count($value)) {
                    $queryBuilder
                        ->andWhere(\sprintf('%s %s (:%s)', $queryField, 'NOT IN', $parameter))
                        ->setParameter($parameter, $value)
                    ;
                }
                break;
            case ListFilter::OPERATOR_GT:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, '>', $parameter))
                    ->setParameter($parameter, $value)
                ;
                break;
            case ListFilter::OPERATOR_GTE:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, '>=', $parameter))
                    ->setParameter($parameter, $value)
                ;
                break;
            case ListFilter::OPERATOR_LT:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, '<', $parameter))
                    ->setParameter($parameter, $value)
                ;
                break;
            case ListFilter::OPERATOR_LTE:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, '<=', $parameter))
                    ->setParameter($parameter, $value)
                ;
                break;
            case ListFilter::OPERATOR_LIKE:
                $queryBuilder
                    ->andWhere(\sprintf('%s %s :%s', $queryField, 'LIKE', $parameter))
                    ->setParameter($parameter, '%'.$value.'%')
                ;
                break;
            default:
                throw new \RuntimeException(\sprintf('Operator "%s" is not supported !', $operator));
        }
    }
741592
public function withFilter(string $name, FilterInterface $filter): Form
    {
        if (! isset($this->filters[$name])) {
            $this->filters[$name] = [];
        }

        $this->filters[$name][] = $filter;

        return $this;
    }
748204
protected function applyFormFilters(QueryBuilder $queryBuilder, array $filters = [])
    {
        foreach ($filters as $field => $listFilter) {
            if (null === $listFilter) {
                continue;
            }

            $this->filterQueryBuilder($queryBuilder, $field, $listFilter);
        }
    }
1014953
private function hasJoin(ProxyQueryInterface $queryBuilder, $alias)
    {
        $joins = $queryBuilder->getDQLPart('join');

        if (!isset($joins[$alias])) {
            return false;
        }

        foreach ($joins[$alias] as $join) {
            if ('trans' === $join->getAlias()) {
                return true;
            }
        }

        return false;
    }
222968
public function apply($name, $value, $query, FilterInterface $filter)
    {
        $column = ( ! empty($this->table) ? $this->table . '.' : null)
            . ( ! empty($this->column) ? $this->column : $name);


        // an array of values: do a whereIn query
        if (is_a($value, Arrayable::class)) $value = $value->toArray();

        if (is_array($value)) {

            $query->whereIn($column, $value);

            return $query;
        }

        // otherwise, do a normal where
        return $query->where($column, $this->operator, $value);
    }
1191299
public function From(string $table, string $alias = ''): QueryBuilder
    {
        $this->table = $table;
        
        if (! empty($alias)) {
            $this->alias = $alias;
        }
        
        return $this;
    }
1153695
protected function appendSortBy($sortBy, $sortMethod, QueryBuilder $queryBuilder, $alias, $locale)
    {
        foreach ($sortBy as $column) {
            if (!in_array(explode('.', $column)[0], $queryBuilder->getAllAliases())) {
                $this->appendSortByJoins($queryBuilder, $alias, $locale);
            }

            $queryBuilder->orderBy($column, $sortMethod);
        }
    }
203503
protected function applyQuerySort(QueryBuilder $queryBuilder, $alias = 'a')
    {
        $sort = $this->get('request')->get('sort');
        $order = $this->get('request')->get('order');

        if ($sort) {
            $repository = $this->get('orm.em')->getRepository($this->getEntityClass());
            // sort using the getQueryBuilderOrderedBy* magic method if defined
            $sortMethodName = 'getQueryBuilderOrderedBy'.ucfirst(strtolower($sort));
            if (method_exists($repository, $sortMethodName)) {
                $queryBuilder = call_user_method_array($sortMethodName, $repository, array($order, $queryBuilder));
            }
            // else sort by property name if exists
            elseif ($sort && property_exists($this->getEntityClass(), $sort)) {
                $queryBuilder->orderBy($alias.'.'.$sort, $order);
            }
        }
        // if empty sort use Gedmo Sortable annotation if enable
        elseif ($sort = $this->getSortableColumn()) {
            // pre order by group if defined
            if ($sortGroup = $this->getSortableGroup()) {
                $queryBuilder->orderBy($alias.'.'.$sortGroup);
            }
            $queryBuilder->addOrderBy($alias.'.'.$sort);
        }

        return $queryBuilder;
    }
1227290
public function applyFiltersToRepository(
        array $filters,
        FilterFactory $filterFactory,
        ConditionAwareRepository $repository
    )
    {
        if (empty($filters) && ($defaultFilter = $filterFactory->getDefaultFilter()) !== null) {
            $filters = ['default' => $defaultFilter];
        }

        foreach ($filters as $filterName => $filterValues) {
            try {
                $filter = $filterFactory->make($filterName, $filterValues);
            } catch (FilterFactoryNotFoundException $e) {
                continue;
            }

            $repository->applyCondition($filter);
        }
    }
236821
protected function addOrderBy(QueryBuilder $queryBuilder, $alias, array $sortBy = [])
    {
        foreach ($sortBy as $field => $order) {
            // if no relation is defined add alias by default
            if (false === strpos($field, '.')) {
                $field = $alias . '.' . $field;
            }

            $queryBuilder->addOrderBy($field, $order);
        }
    }