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 32 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{ "name": "Andreas Braun", "email": "[email protected]" }
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome back! 🎉

What happened here? We're definitely not going to make this bump for 2.5.x

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i roll it back, i skipped a bit of digging. And tought you were aiming 8.1 by using ReturnTypeWillChange attribute. And then i read a ref about this and saw it's design for backward compatibility xD

But i got the same error trigger localy as in the workflow about the "ReturnTypeWillChange does not exist", but after updating my php interpreter and extensions used for running phpstan/psalm localy thoses errors aren't triggered anymore. I doesn't have much knowledge around coding standard etc .. (i'm working alone for 15 years now, and unfortunatly doesn't had the occasion to pratice that part) So i'm reading a lot and try to finish this PR the correct way, but i'm really new to team work with this "level" of standard/workflow, and it's a bit rough :p, i'm currently reading psalm docs to not fucked up things. But i feel a bit out of my league on this whole subjects. Not sure i'm doing the correct way (and spam commits while i understand what i'm doing wrong or good xD)

"ext-mongodb": "^1.5",
"doctrine/annotations": "^1.12",
"doctrine/cache": "^1.11 || ^2.0",
Expand Down
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
23 changes: 22 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
39 changes: 36 additions & 3 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 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
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
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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

use function count;
use function current;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Doctrine\ODM\MongoDB\UnitOfWork;
use Generator;
use Iterator;
use ReturnTypeWillChange;
use RuntimeException;
use Traversable;
use ReturnTypeWillChange;

/**
* Iterator that wraps a traversable and hydrates results into objects
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/UnrewindableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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

use function iterator_to_array;
use function sprintf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
use ReturnTypeWillChange;
use Traversable;
use ReturnTypeWillChange;

use function array_combine;
use function array_diff_key;
Expand Down
41 changes: 38 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 All @@ -25,6 +26,7 @@
use function is_callable;
use function is_string;
use function strtolower;
use function trigger_deprecation;

/**
* Query builder for ODM.
Expand Down Expand Up @@ -356,6 +358,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 @@ -483,13 +496,35 @@ public function exists(bool $bool): self

/**
* Create a new Expr instance that can be used as an expression with the Builder
*
* @deprecated use createQueryExpression instead
*/
public function expr(): Expr
{
$expr = new Expr($this->dm);
$expr->setClassMetadata($this->class);
trigger_deprecation(
'doctrine/mongodb-odm',
'2.5',
'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.
*
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
* @see Aggregation\Expr::aggregationExpression()
*
* @param array|Aggregation\Expr $expression
*/
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
4 changes: 1 addition & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* 1: mixed
* }
* @psalm-type Hints = array<int, mixed>
* @psalm-type CommitOptions array{
* @psalm-type CommitOptions = array{
* fsync?: bool,
* safe?: int,
* w?: int,
Expand Down Expand Up @@ -1601,7 +1601,6 @@ public function removeFromIdentityMap(object $document): bool
* @throws InvalidArgumentException If the class does not have an identifier.
*
* @template T of object
*
* @psalm-suppress InvalidReturnStatement, InvalidReturnType because of the inability of defining a generic property map
*/
public function getById($id, ClassMetadata $class): object
Expand Down Expand Up @@ -1630,7 +1629,6 @@ public function getById($id, ClassMetadata $class): object
* @throws InvalidArgumentException If the class does not have an identifier.
*
* @template T of object
*
* @psalm-suppress InvalidReturnStatement, InvalidReturnType because of the inability of defining a generic property map
*/
public function tryGetById($id, ClassMetadata $class)
Expand Down