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

Feature Criteria merge #367

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from
Open
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Doctrine\Common\Collections\Expr\Expression;
use Doctrine\Deprecations\Deprecation;

use function array_filter;
use function array_map;
use function array_merge;
use function func_num_args;
use function strtoupper;

Expand Down Expand Up @@ -54,6 +56,20 @@ public static function expr()
return self::$expressionBuilder;
}

/**
* Merges two Criteria together.
*
* @return self
*/
public static function merge(Criteria $leftCriteria, Criteria $rightCriteria)
{
return self::create()
->andWhere(self::expr()->andX(...array_filter([$leftCriteria->getWhereExpression(), $rightCriteria->getWhereExpression()])))
->orderBy(array_merge($leftCriteria->getOrderings(), $rightCriteria->getOrderings()))
->setFirstResult($rightCriteria->getFirstResult() ?? $leftCriteria->getFirstResult())
->setMaxResults($rightCriteria->getMaxResults() ?? $leftCriteria->getFirstResult());
Copy link

@OleksiiBulba OleksiiBulba Jul 19, 2023

Choose a reason for hiding this comment

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

@TheDutchScorpion, is it a typo or is it intended?

Suggested change
->setMaxResults($rightCriteria->getMaxResults() ?? $leftCriteria->getFirstResult());
->setMaxResults($rightCriteria->getMaxResults() ?? $leftCriteria->getMaxResults());

}

/**
* Construct a new Criteria.
*
Expand Down