Skip to content
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

Support expr in queries #2343

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 31 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
Protocteur Nov 4, 2022
a9b8dd6
Merge remote-tracking branch 'origin/support-expr-in-queries' into su…
Protocteur Nov 4, 2022
9746da5
+ applied vendor/bin/phpcbf
Protocteur Nov 4, 2022
a28c84c
+ minors fixes for vendor/bin/phpstan validation
Protocteur Nov 4, 2022
771b43d
+ rollback some unexpected changes in previous commits
Protocteur Nov 5, 2022
eb60a6f
+ replaced Query\Builder::expr() calls to Query\Builder::createQueryE…
Protocteur Nov 6, 2022
b5b2a82
+ adding regexMatch to Aggretation
Protocteur 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
*/
Protocteur marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 0 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Event/PreUpdateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ODM\MongoDB\Event;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\UnitOfWork;
use InvalidArgumentException;

use function get_class;
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Doctrine\ODM\MongoDB\Hydrator;

use Doctrine\ODM\MongoDB\UnitOfWork;

/**
* The HydratorInterface defines methods all hydrator need to implement
*
Expand Down
1 change: 0 additions & 1 deletion 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
1 change: 0 additions & 1 deletion 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
1 change: 0 additions & 1 deletion 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use Closure;
use Doctrine\Common\Collections\Collection as BaseCollection;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
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
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
3 changes: 0 additions & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Doctrine\Persistence\PropertyChangedListener;
use InvalidArgumentException;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\WriteConcern;
use ProxyManager\Proxy\GhostObjectInterface;
use ReflectionProperty;
use UnexpectedValueException;
Expand Down Expand Up @@ -1601,7 +1600,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 +1628,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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Tests\BaseTest;

class GH1152Test extends BaseTest
Expand Down