Skip to content

Support expr in queries #2343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: 2.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5264e26
+ implement $expr operator as exprOp method for query and aggregation…
Jul 17, 2021
4725c19
* fix MatchStage expr revision introduced bug
Jul 17, 2021
0a38fa5
+ add exprOp test
Jul 18, 2021
c319eca
+ fix exprOp test
Jul 18, 2021
7ba6315
+ fix exprOp test
Jul 18, 2021
61aafa3
Merge remote-tracking branch 'origin/2.3.x' into support-expr-in-queries
Jan 31, 2022
06a95f1
+ update methods name refactoring as suggested in PR #2343 (exprOr to…
Jan 31, 2022
3de0efb
Merge branch '2.3.x' into support-expr-in-queries
Jan 31, 2022
fe79bbd
+ used phpcbf on /lib
Mar 8, 2022
898ce49
+ used phpcbf on /tests
Mar 8, 2022
5b6a368
+ implement $expr operator as exprOp method for query and aggregation…
Jul 17, 2021
e865bd0
* fix MatchStage expr revision introduced bug
Jul 17, 2021
4285401
+ add exprOp test
Jul 18, 2021
eece492
+ fix exprOp test
Jul 18, 2021
8e4ea1b
+ fix exprOp test
Jul 18, 2021
ccaa1a4
+ update methods name refactoring as suggested in PR #2343 (exprOr to…
Jan 31, 2022
110375e
+ rebase on newer 2.3.x
Mar 8, 2022
caf12dd
Merge remote-tracking branch 'fork/support-expr-in-queries' into supp…
Mar 8, 2022
d1660cd
- remove test on previously removed method (aggregationExpression)
Mar 8, 2022
0b458de
+ implement $expr operator as exprOp method for query and aggregation…
Jul 17, 2021
839adc2
* fix MatchStage expr revision introduced bug
Jul 17, 2021
fb14785
+ add exprOp test
Jul 18, 2021
906d5bc
+ fix exprOp test
Jul 18, 2021
67e6913
+ fix exprOp test
Jul 18, 2021
b387bff
+ update methods name refactoring as suggested in PR #2343 (exprOr to…
Jan 31, 2022
372a27b
+ rebase on newer 2.3.x
Mar 8, 2022
9d5845c
+ used phpcbf on /tests
Mar 8, 2022
c30899b
- remove test on previously removed method (aggregationExpression)
Mar 8, 2022
a3dfb0d
+ minor documentation updates
vincent-le-henaff Nov 4, 2022
a9b8dd6
Merge remote-tracking branch 'origin/support-expr-in-queries' into su…
vincent-le-henaff Nov 4, 2022
9746da5
+ applied vendor/bin/phpcbf
vincent-le-henaff Nov 4, 2022
a28c84c
+ minors fixes for vendor/bin/phpstan validation
vincent-le-henaff Nov 4, 2022
771b43d
+ rollback some unexpected changes in previous commits
vincent-le-henaff Nov 5, 2022
eb60a6f
+ replaced Query\Builder::expr() calls to Query\Builder::createQueryE…
vincent-le-henaff Nov 6, 2022
b5b2a82
+ adding regexMatch to Aggretation
vincent-le-henaff Nov 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public function count(string $fieldName): Stage\Count
return $stage;
}

/**
* Create a new Expr instance that can be used as an expression with the Builder
*/
public function createAggregationExpression(): Expr
{
return new Expr($this->dm, $this->class);
}

/**
* Executes the aggregation pipeline
*
Expand All @@ -180,9 +188,20 @@ public function execute(array $options = []): Iterator
return $this->getAggregation($options)->getIterator();
}

/**
* @deprecated use createAggregationExpression instead
*/
public function expr(): Expr
{
return new Expr($this->dm, $this->class);
trigger_deprecation(
'doctrine/mongodb-odm',
'2.5',
'The "%s" method is deprecated. Please use "%s::createAggregationExpression" instead.',
__METHOD__,
static::class
);

return $this->createAggregationExpression();
}

/**
Expand Down
37 changes: 36 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function is_array;
use function is_string;
use function substr;
use function trigger_deprecation;

/**
* Fluent interface for building aggregation pipelines.
Expand Down Expand Up @@ -324,6 +325,16 @@ public static function convertExpression($expression)
return $expression;
}

/**
* Returns a new expression object
*
* @return static
*/
public function createAggregationExpression(): self
{
return new static($this->dm, $this->class);
}

/**
* Converts a date object to a string according to a user-specified format.
*
Expand Down Expand Up @@ -453,10 +464,20 @@ public function exp($exponent): self

/**
* Returns a new expression object
*
* @deprecated use createAggregationExpression instead
*/
public function expr(): self
{
return new static($this->dm, $this->class);
trigger_deprecation(
'doctrine/mongodb-odm',
'2.5',
'The "%s" method is deprecated. Please use "%s::createAggregationExpression" instead.',
__METHOD__,
static::class
);

return $this->createAggregationExpression();
}

/**
Expand Down Expand Up @@ -1098,6 +1119,20 @@ public function reduce($input, $initialValue, $in): self
return $this->operator('$reduce', ['input' => $input, 'initialValue' => $initialValue, 'in' => $in]);
}

/**
* Performs a regular expression (regex) pattern matching
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/regexMatch/
*
* @param mixed|self $input The value to compare. (Use "$<field>" format to test your regex on a field)
* @param mixed|self $regex The regular expression (regex)
* @param mixed|self $options A string containing regex flags options. See mongodb doc. above for details
*/
public function regexMatch($input, $regex, $options): self
{
return $this->operator('$regexMatch', ['input' => $input, 'regex' => $regex, 'options' => $options]);
}

/**
* Accepts an array expression as an argument and returns an array with the
* elements in reverse order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(Builder $builder, Stage\AbstractBucket $bucket)
parent::__construct($builder);

$this->bucket = $bucket;
$this->expr = $builder->expr();
$this->expr = $builder->createAggregationExpression();
}

public function getExpression(): array
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(Builder $builder)
{
parent::__construct($builder);

$this->expr = $builder->expr();
$this->expr = $builder->createAggregationExpression();
}

public function getExpression(): array
Expand Down
41 changes: 37 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

use Doctrine\ODM\MongoDB\Aggregation;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Stage;
use Doctrine\ODM\MongoDB\Query\Expr;
use GeoJson\Geometry\Geometry;

use function func_get_args;
use function trigger_deprecation;

/**
* Fluent interface for building aggregation pipelines.
Expand All @@ -23,7 +25,7 @@ public function __construct(Builder $builder)
{
parent::__construct($builder);

$this->query = $this->expr();
$this->query = $this->createQueryExpression();
}

/**
Expand Down Expand Up @@ -114,6 +116,15 @@ public function all(array $values): self
return $this;
}

/**
* Create a new Expr instance that can be used to build partial expressions
* for other operator methods.
*/
public function createQueryExpression(): Expr
{
return $this->builder->matchExpr();
}

/**
* Specify $elemMatch criteria for the current field.
*
Expand Down Expand Up @@ -166,12 +177,34 @@ public function exists(bool $bool): self
}

/**
* Create a new Expr instance that can be used to build partial expressions
* for other operator methods.
* @deprecated use createExpr instead
*/
public function expr(): Expr
{
return $this->builder->matchExpr();
trigger_deprecation(
'doctrine/mongodb-odm',
'2.5',
'The "%s" method is deprecated. Please use "%s::createQueryExpression" instead.',
__METHOD__,
static::class
);

return $this->createQueryExpression();
}

/**
* Specify $expr criteria for the current field.
*
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
* @see Expr::aggregationExpression()
*
* @param array|Aggregation\Expr $expression
*/
public function aggregationExpression($expression): self
{
$this->query->aggregationExpression($expression);

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(Builder $builder)
{
parent::__construct($builder);

$this->expr = $builder->expr();
$this->expr = $builder->createAggregationExpression();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class Configuration
* persistentCollectionGenerator?: PersistentCollectionGenerator,
* persistentCollectionDir?: string,
* persistentCollectionNamespace?: string,
* repositoryFactory?: RepositoryFactory
* repositoryFactory?: RepositoryFactory,
* writeConcern?: WriteConcern
* }
*/
private array $attributes = [];
Expand Down
1 change: 0 additions & 1 deletion lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ public function getClassNameResolver(): ClassNameResolver
* @psalm-return ClassMetadata<T>
*
* @template T of object
*
* @psalm-suppress InvalidReturnType, InvalidReturnStatement see https://github.com/vimeo/psalm/issues/5788
*/
public function getClassMetadata($className): ClassMetadata
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Countable;
use Generator;
use ReturnTypeWillChange;
use RuntimeException;
use Traversable;

Expand Down Expand Up @@ -80,7 +79,7 @@ public function toArray(): array
/**
* @return TValue|false
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function current()
{
return current($this->items);
Expand All @@ -89,7 +88,7 @@ public function current()
/**
* @return mixed
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function key()
{
return key($this->items);
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\ODM\MongoDB\UnitOfWork;
use Generator;
use Iterator;
use ReturnTypeWillChange;
use RuntimeException;
use Traversable;

Expand Down Expand Up @@ -60,7 +59,7 @@ public function __destruct()
/**
* @return TDocument|null
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function current()
{
return $this->hydrate($this->getIterator()->current());
Expand All @@ -69,7 +68,7 @@ public function current()
/**
* @return mixed
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function key()
{
return $this->getIterator()->key();
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Iterator/PrimingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Query\ReferencePrimer;
use Doctrine\ODM\MongoDB\UnitOfWork;
use ReturnTypeWillChange;

use function is_callable;
use function iterator_to_array;
Expand Down Expand Up @@ -62,7 +61,7 @@ public function toArray(): array
/**
* @return TValue|null
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function current()
{
$this->primeReferences();
Expand All @@ -78,7 +77,7 @@ public function next(): void
/**
* @return mixed
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function key()
{
return $this->iterator->key();
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Iterator/UnrewindableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use LogicException;
use ReturnTypeWillChange;
use RuntimeException;
use Traversable;

Expand Down Expand Up @@ -61,7 +60,7 @@ public function toArray(): array
/**
* @return TValue|null
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function current()
{
return $this->getIterator()->current();
Expand All @@ -70,7 +69,7 @@ public function current()
/**
* @return mixed
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function key()
{
if ($this->iterator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
use ReturnTypeWillChange;
use Traversable;

use function array_combine;
Expand Down Expand Up @@ -410,7 +409,7 @@ public function getValues()
/**
* @return int
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function count()
{
// Workaround around not being able to directly count inverse collections anymore
Expand Down Expand Up @@ -438,7 +437,7 @@ public function isEmpty()
* @return Traversable
* @psalm-return Traversable<TKey, T>
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function getIterator()
{
$this->initialize();
Expand Down Expand Up @@ -537,7 +536,7 @@ public function __sleep()
*
* @return bool
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
$this->initialize();
Expand All @@ -551,7 +550,7 @@ public function offsetExists($offset)
* @return mixed
* @psalm-return T|null
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$this->initialize();
Expand All @@ -565,7 +564,7 @@ public function offsetGet($offset)
*
* @return void
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (! isset($offset)) {
Expand All @@ -582,7 +581,7 @@ public function offsetSet($offset, $value)
*
* @return void
*/
#[ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->doRemove($offset, true);
Expand Down
Loading