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 8 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 @@ -167,6 +167,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 @@ -185,9 +193,20 @@ public function execute(array $options = []): Iterator
return $this->getAggregation($options)->getIterator();
}

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

return $this->createAggregationExpression();
}

/**
Expand Down
22 changes: 21 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,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 @@ -459,10 +469,20 @@ public function exp($exponent): self

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

return $this->createAggregationExpression();
}

/**
Expand Down
38 changes: 35 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;
Expand Down Expand Up @@ -112,6 +113,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 @@ -164,12 +174,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.3',
'The "%s" method is deprecated. Please use "%s::createQueryExpression" instead.',
__METHOD__,
static::class
);

return $this->createQueryExpression();
}

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

return $this;
}

/**
Expand Down
40 changes: 37 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Query;

use BadMethodCallException;
use Doctrine\ODM\MongoDB\Aggregation;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use GeoJson\Geometry\Geometry;
Expand Down Expand Up @@ -366,6 +367,17 @@ public function count(): self
return $this;
}

/**
* Create a new Expr instance that can be used as an expression with the Builder
*/
public function createQueryExpression(): Expr
{
$expr = new Expr($this->dm);
$expr->setClassMetadata($this->class);

return $expr;
}

/**
* Sets the value of the current field to the current date, either as a date or a timestamp.
*
Expand Down Expand Up @@ -493,13 +505,35 @@ public function exists(bool $bool): self

/**
* Create a new Expr instance that can be used as an expression with the Builder
*
* @deprecated use createExpr instead
*/
public function expr(): Expr
{
$expr = new Expr($this->dm);
$expr->setClassMetadata($this->class);
trigger_deprecation(
'doctrine/mongodb-odm',
'2.3',
'The "%s" method is deprecated. Please use "%s::createQueryExpression" instead.',
__METHOD__,
static::class
);

return $expr;
return $this->createQueryExpression();
}

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

return $this;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Query;

use BadMethodCallException;
use Doctrine\ODM\MongoDB\Aggregation;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
Expand Down Expand Up @@ -404,6 +405,19 @@ public function elemMatch($expression): self
return $this->operator('$elemMatch', $expression);
}

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

/**
* Specify an equality match for the current field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public static function provideExpressionOperators(): array
'operator' => 'exp',
'args' => ['$field'],
],
'aggregationExpression' => [
'expected' => ['$expr' => ['$eq' => ['$field', '$otherField']]],
'operator' => 'aggregationExpression',
'args' => [['$eq' => ['$field', '$otherField']]],
],
'filter' => [
'expected' => ['$filter' => ['input' => '$array', 'as' => '$as', 'cond' => '$cond']],
'operator' => 'filter',
Expand Down