Apply Expr() object in query builder with expression value

@param QueryBuilder|Builder $qb
@param string $alias
@param string $field
@param string $exprValue
@param string $namedParameter
@throws \InvalidArgumentException
@throws \RuntimeException
199546
public function applyExprFromString($qb, $alias, $field, $exprValue, $namedParameter = null)
	{
		if (!$qb instanceof QueryBuilder && !$qb instanceof Builder) {
			throw new \InvalidArgumentException(sprintf('Argument 1 passed to "%s" must be an instance of "%s" or "%s", "%s" given.', __METHOD__, '\Doctrine\ORM\QueryBuilder', '\Doctrine\ODM\MongoDB\Query\Builder', gettype($qb)));
		}
		
		$value = $exprValue;
		$namedParameter = $namedParameter ?: ':'.$field;
		$dbDriver = $qb instanceof QueryBuilder ? 'orm' : 'mongodb';
		
		foreach ($this->mapConverters as $mapConverter) {
	        if (true === $this->supports($mapConverter, $dbDriver, $exprValue)) {
	            $converter = new $mapConverter['class']();
	            
	            if (!$converter instanceof QueryExprConverterInterface) {
	                throw new \RuntimeException(sprintf('Class "%s" must implemented interface "%s" for be used like query expression value converter.', $mapConverter['class'], '\Oka\PaginationBundle\Converter\QueryExprConverterInterface'));
	            }
	            
	            $expr = $converter->apply($dbDriver, $alias, $field, $exprValue, $namedParameter, $value);
	            break;
	        }
		}
		
		if ($qb instanceof QueryBuilder) {
			if (false === isset($expr)) {
				$expr = (new \Doctrine\ORM\Query\Expr())->eq($alias.'.'.$field, $namedParameter);
				$qb->setParameter($namedParameter, $exprValue);
			} else {
			    switch (true) {
			        case is_array($value):
			            foreach ($value as $key => $val) {
			                $qb->setParameter($key, $val);
			            }
			            break;
			        case null !== $value:
			            $qb->setParameter($namedParameter, $value);
			            break;
			    }
			}
			
			$qb->andWhere($expr);
		} else {
			$qb->addAnd(isset($expr) ? $expr : (new \Doctrine\MongoDB\Query\Expr())->field($field)->equals($value));
		}
	}
1063337
public function getFilter(QueryBuilder $qb, $dqlAlias)
    {
        if (null !== $this->dqlAlias) {
            $dqlAlias = $this->dqlAlias;
        }

        $field = ArgumentToOperandConverter::toField($this->field);
        $value = ArgumentToOperandConverter::toValue($this->value);

        return (string) new DoctrineComparison(
            $field->transform($qb, $dqlAlias),
            $this->operator,
            $value->transform($qb, $dqlAlias)
        );
    }
274206
public function transform(QueryBuilder $qb, $dqlAlias)
    {
        $field = ArgumentToOperandConverter::toField($this->field);
        $value = ArgumentToOperandConverter::toValue($this->value);

        $field = $field->transform($qb, $dqlAlias);
        $value = $value->transform($qb, $dqlAlias);

        return sprintf('(%s %s %s)', $field, $this->operation, $value);
    }
803257
function generateNextTemp(\Doctrine\ORM\QueryBuilder $qb,$field)
    {
        $temporaryMask = $this->getTemporaryMask().'-{000}';
        return $this->generate($qb,$temporaryMask ,$field,self::MODE_NEXT);
    }
823780
public function field($field, $alias = null) {
        if (is_string($field)) {
            $fields = explode(',', trim($field));
            foreach ($fields as $field) {
                $this->fields [] = Condition::cleanField($field . ($alias ? ' AS ' . $alias : ''));
            }
        } else if ($field instanceof Query) {
            if ($alias) {
                $field->alias($alias);
            }
            $this->fields [] = $field;
        } else if ($field instanceof ImmutableValue) {
            if ($alias) {
                $field->alias($alias);
            }
            $this->fields [] = $field;
        }

        return $this;
    }
388624
public function applyFilters(Builder $query, string $alias = null)
    {
        foreach ($this->getFilters() as $filter) {
            $filter->apply($query, $alias);
        }
    }
742586
public static function filterLike(QueryBuilder $queryBuilder, &$criteria, $field, $alias = 'o')
    {
        if (isset($criteria[$field])) {
            if (!empty($criteria[$field])) {
                $queryBuilder->andWhere(sprintf('%s.%s LIKE :%s', $alias, $field, $field))->setParameter(
                    $field,
                    '%'.$criteria[$field].'%'
                );
            }
            unset($criteria[$field]);
        }

        return $criteria;
    }
486986
protected static function _bindParam(\Mmi\Orm\Query $q, $name, $value)
    {
        if ($value === true) {
            $q->andField($name)->notEquals(null);
            return;
        }
        $q->andField($name)->equals($value);
    }
947403
private function appendRelationAnd(QueryBuilder $queryBuilder, $relation, $values, $alias)
    {
        $parameter = [];
        $expr = $queryBuilder->expr()->andX();

        $length = count($values);
        for ($i = 0; $i < $length; ++$i) {
            $queryBuilder->leftJoin($relation, $alias . $i);

            $expr->add($queryBuilder->expr()->eq($alias . $i . '.id', ':' . $alias . $i));

            $parameter[$alias . $i] = $values[$i];
        }
        $queryBuilder->andWhere($expr);

        return $parameter;
    }
42148
protected function parseExtendedSearchFilters($parseFilters, $qb, $expressions, &$parameters)
    {
        foreach ($parseFilters as $f) {
            if (isset($f->children)) {
                list($expr, $params) = $this->addExtendedAdvancedSearchWhereClause($qb, $f);
            } else {
                if (!empty($f->command)) {
                    // is this an Extended Field Filter?
                    if (in_array($f->command, array_keys($this->extendedFieldFilters))) {
                        // Change the where clause to use the extended table alias.
                        $extendedFilter = $this->extendedFieldFilters[$f->command];
                        $schema         = $this->leadFieldModel->getSchemaDefinition(
                            $extendedFilter['alias'],
                            $extendedFilter['type']
                        );
                        $secure              = 'extendedFieldSecure' === $extendedFilter['object'] ? '_secure' : '';
                        $tableAlias          = $schema['type'].$secure.$extendedFilter['id'];
                        list($expr, $params) = $this->addStandardCatchAllWhereClause($qb, $f, [$tableAlias.'.value']);
                    } elseif ($this->isSupportedSearchCommand($f->command, $f->string)) {
                        list($expr, $params) = $this->addSearchCommandWhereClause($qb, $f);
                    } else {
                        //treat the command:string as if its a single word
                        $f->string           = $f->command.':'.$f->string;
                        $f->not              = false;
                        $f->strict           = true;
                        list($expr, $params) = $this->addCatchAllWhereClause($qb, $f);
                    }
                } else {
                    list($expr, $params) = $this->addCatchAllWhereClause($qb, $f);
                }
            }
            if (!empty($params)) {
                $parameters = array_merge($parameters, $params);
            }

            $this->appendExpression($expressions, $expr);
        }
    }
1261530
private function applyFilter(Builder $query, $field, $filter, $or = false)
    {
        $filter = explode(':', $filter);
        if (count($filter) > 1) {
            $operator = $this->getFilterOperator($filter[0]);
            $value = $this->replaceWildcards($filter[1]);
        }
        else {
            $operator = '=';
            $value = $this->replaceWildcards($filter[0]);
        }

        $fields = explode('.', $field);
        if (count($fields) > 1) {
            return $this->applyNestedFilter($query, $fields, $operator, $value, $or);
        }
        else {
            return $this->applyWhereClause($query, $field, $operator, $value, $or);
        }
    }
972937
protected function getFieldQuery ($field, $searchOper, $term, QueryBuilder $qb)
    {
        if(is_array($this->searchableProperties) && !in_array($field, $this->searchableProperties)){
            throw new \Exception(sprintf('Field %s is not searchable', $field));
        }

        switch ($searchOper) {
            case 'eq':
                return $qb->expr()->eq($field, $qb->expr()->literal($term));
                 
            case 'ne':
                return $qb->expr()->neq($field, $qb->expr()->literal($term));
                 
            case 'lt':
                return $qb->expr()->lt($field, $qb->expr()->literal($term));
                 
            case 'le':
                return $qb->expr()->lte($field, $qb->expr()->literal($term));
                 
            case 'gt':
                return $qb->expr()->gt($field, $qb->expr()->literal($term));
                 
            case 'ge':
                return $qb->expr()->gte($field, $qb->expr()->literal($term));
                 
            case 'bw':
                return $qb->expr()->like($field, $qb->expr()->literal($term . '%'));
    
            case 'bn':
                return $qb->expr()->not($qb->expr()->like($field, $qb->expr()->literal($term . '%')));
                 
            case 'ew':
                return $qb->expr()->like($field, $qb->expr()->literal('%' . $term));
    
            case 'en':
                return $qb->expr()->not($qb->expr()->like($field, $qb->expr()->literal('%' . $term)));
    
            case 'cn':
                return $qb->expr()->like($field, $qb->expr()->literal('%' . $term . '%'));
    
            case 'nc':
                return $qb->expr()->not($qb->expr()->like($field, $qb->expr()->literal('%' . $term . '%')));
    
            default:
                throw new \InvalidArgumentException(sprintf('Search operator %s is not valid', $searchOper));
        }
    }
1168723
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
    {
        if (!$data || !is_array($data) || !array_key_exists('value', $data) || !$data['value']['Value']) {
            return;
        }

        $operator = '=';
        $value = $data['value']['Value'];
        $name = $this->getName();

        $queryBuilder->entityJoin(array(
            $name => array(
                'fieldName' => $name,
            ), )
        );

        if ($data['type'] == 1) {
            $operator = 'LIKE';
            $value = '%' . $value . '%';
        } elseif ($data['type'] == 2) {
            $operator = 'NOT LIKE';
            $value = '%' . $value . '%';
        }

        $this->applyWhere($queryBuilder, sprintf('s_%s.field = :field', $name));
        $queryBuilder->setParameter('field', $data['value']['Field']);

        $this->applyWhere($queryBuilder, sprintf('s_%s.value %s :value', $name, $operator));
        $queryBuilder->setParameter('value', $value);
    }
1037419
private function applyFilters (QueryBuilder $qb, array $filters)
    {

        if (!isset($filters['groupOp']) || !in_array($filters['groupOp'], array('AND', 'OR'))){
            throw new \InvalidArgumentException('Operator does not match OR | AND');
        }

        if (!isset($filters['rules']) || !is_array($filters['rules'])){
            throw new \InvalidArgumentException('Rules are not set.');
        }
        
        $groupOp = $filters['groupOp'];
    
        $andXWithWhere = $qb->expr()->andX();
        $andXWithHaving = $qb->expr()->andX();
        
        $orXWithWhere = $qb->expr()->orX();
        $orXWithHaving = $qb->expr()->orX();

        $supportedAggregateOperators = array('eq', 'ne', 'lt', 'le', 'gt', 'ge');
    
        foreach ($filters['rules'] as $rule) {
            $rule = $this->getResolvedRule((array) $rule);
            
            $field = $this->getFieldQuery($rule['field'], $rule['op'], $rule['data'], $qb);
            $isAgrigated = $this->isAggregated($rule['field']);
            
            if ($groupOp === 'AND'){
                if (false === $isAgrigated){
                    $andXWithWhere->add($field);
                } else if ($isAgrigated && in_array($rule['op'], $supportedAggregateOperators)){
                    $andXWithHaving->add($field);
                }  
            } elseif ($groupOp === 'OR'){
                if (false === $isAgrigated){
                    $orXWithWhere->add($field);
                } else if ($isAgrigated && in_array($rule['op'], $supportedAggregateOperators)){
                    $orXWithHaving->add($field);
                }
            }
        }
    
        
          
        (count($andXWithWhere->getParts()) > 0) ? $qb->andWhere($andXWithWhere) : null;
        (count($andXWithHaving->getParts()) > 0) ? $qb->andHaving($andXWithHaving) : null; 
        
        // we use addWhere/addHaving because if we add a where/having clause beforehand then it will be ignored.
        (count($orXWithWhere->getParts()) > 0) ? $qb->andWhere($orXWithWhere) : null;
        (count($orXWithHaving->getParts()) > 0) ? $qb->andHaving($orXWithHaving) : null;     
    }
539756
public function getWhereConditionExpr(Select $select, string $tableAlias, string $operator, $value) : Expr
    {
        $operand = $this->getExpressionInSelect($select, $tableAlias);

        if ($value === null) {
            if ($operator === ConditionOperator::EQUALS) {
                return Expr::isNull($operand);
            } elseif ($operator === ConditionOperator::NOT_EQUALS) {
                return Expr::isNotNull($operand);
            } else {
                throw InvalidArgumentException::format(
                    'Cannot use operator \'%s\' with NULL value, only (%s) are supported',
                    $operator, Debug::formatValues([ConditionOperator::EQUALS, ConditionOperator::NOT_EQUALS])
                );
            }
        } elseif ($operator === ConditionOperator::IN || $operator == ConditionOperator::NOT_IN) {
            return new BinOp(
                $operand,
                $this->mapConditionOperator($operator),
                Expr::tupleParams(null, $value)
            );
        } else {
            return new BinOp(
                $operand,
                $this->mapConditionOperator($operator),
                Expr::param(null, $value)
            );
        }
    }
389392
protected function constraintField(ConstraintFactory $where, $field, $value, $alias)
    {
        if ($field === $this->class->nodename) {
            $where->eq()->name($alias)->literal($value);
        } else {
            $where->eq()->field($alias.'.'.$field)->literal($value);
        }
    }
500595
protected function buildAssociationQuery($qb, $assoc, $alias, $params)
    {
        $data = $this->em->getClassMetadata($assoc);
        foreach ($params as $key => $val) {
            if (isset($data->associationMappings)) {
                if ($map = array_key_exists($key, $data->associationMappings)) {
                    if (is_array($val)) {
                        $qb->innerJoin("$alias.$key", "_$key");
                        foreach ($val as $column => $v) {
                            if (is_array($v)) {
                                $this->buildAssociationQuery($qb, $map['targetEntity'], $column, $v);
                                continue;
                            }
                            $paramname = "_$key" . '__' . $column;
                            $qb->andWhere("_$key.$column = :$paramname");
                            $qb->setParameter($paramname, $v);
                        }
                        continue;
                    }
                }
            }
            if ($val === null) {
                $qb->andWhere("s.$key IS NULL");
            } else {
                $paramname = str_replace(".", "", "s_$key");
                $qb->andWhere("s.$key = :$paramname");
                $qb->setParameter($paramname, $val);
            }
        }
    }