Skip to content

Commit

Permalink
dbal 5.0: implement processing of unique joins
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Aug 14, 2021
1 parent fec6d73 commit 03be1fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"nette/caching": "~2.5 || ~3.0",
"nette/utils": "~2.5 || ~3.0",
"nette/tokenizer": "~2.3 || ~3.0",
"nextras/dbal": "~4.0"
"nextras/dbal": "~5.0@dev"
},
"require-dev": {
"nette/bootstrap": "~2.4 || ~3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function findBy(array $conds): ICollection
$expression = $collection->getHelper()->processFilterFunction($collection->queryBuilder, $conds, null);
$expression = $expression->applyAggregator($collection->queryBuilder);

foreach ($expression->joins as $join) {
foreach ($expression->getUniqueJoins($collection->queryBuilder) as $join) {
$join->applyJoin($collection->queryBuilder);
}

Expand Down
23 changes: 23 additions & 0 deletions src/Collection/Helpers/DbalExpressionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nextras\Orm\Collection\Helpers;


use MongoDB\Driver\Query;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -117,4 +118,26 @@ public function applyAggregator(QueryBuilder $queryBuilder): DbalExpressionResul

return $this->aggregator->aggregate($queryBuilder, $this);
}


/**
* @return array<DbalJoinEntry>
*/
public function getUniqueJoins(QueryBuilder $queryBuilder): array
{
$known = [];
foreach ($queryBuilder->getClause('join')[0] ?? [] as $join) {
$known[$join['table'] . $join['on']] = true; // $from$on
}
$missing = [];
foreach ($this->joins as $join) {
$key = "$join->toExpression AS %table" . $join->onExpression;
if (isset($known[$key])) {
continue;
}
$known[$key] = true;
$missing[] = $join;
}
return $missing;
}
}
2 changes: 1 addition & 1 deletion src/Collection/Helpers/DbalQueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function processFilterFunction(
public function processOrder(QueryBuilder $builder, $expr, string $direction): void
{
$expressionResult = $this->processPropertyExpr($builder, $expr);
foreach ($expressionResult->joins as $join) {
foreach ($expressionResult->getUniqueJoins($builder) as $join) {
$join->applyJoin($builder);
}
$orderingExpression = $this->processOrderDirection($expressionResult, $direction);
Expand Down

0 comments on commit 03be1fa

Please sign in to comment.