Skip to content

Commit

Permalink
Merge pull request #401 from nextras/count-bugfix
Browse files Browse the repository at this point in the history
relationships: fixed counting entities when multiplied by joins
  • Loading branch information
hrach committed Apr 6, 2020
2 parents 6eee323 + 75f70c5 commit 1fc1c9c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/RelationshipMapperManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private function fetchCounts(QueryBuilder $builder, array $values)
$result = $this->processMultiCountResult($builder, $values);

} else {
$builder->addSelect('COUNT(%column) as count', "$targetTable.$this->primaryKeyTo");
$builder->addSelect('COUNT(DISTINCT %column) AS [count]', "$targetTable.$this->primaryKeyTo");
$builder->orderBy(null);
$builder->andWhere('%column IN %any', "$targetTable.$this->primaryKeyFrom", $values);
$builder->groupBy('%column', "$targetTable.$this->primaryKeyFrom");
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/RelationshipMapperOneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function fetchCounts(QueryBuilder $builder, array $values)

} else {
$builder->orderBy(null);
$builder->addSelect('COUNT(%column) AS [count]', "{$sourceTable}.{$targetStoragePrimaryKey}");
$builder->addSelect('COUNT(DISTINCT %column) AS [count]', "{$sourceTable}.{$targetStoragePrimaryKey}");
$builder->andWhere('%column IN %any', "{$sourceTable}.{$this->joinStorageKey}", $values);
$builder->groupBy('%column', "{$sourceTable}.{$this->joinStorageKey}");
$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ class RelationshipManyHasManyTest extends DataTestCase

Assert::same(1, \count($tag->books));
}


public function testCountStoredOnManyHasManyRelationshipCondition()
{
$tag = $this->orm->tags->getByIdChecked(1);
$books = $tag->books->toCollection()->findBy([
'author->id' => 1,
]);
Assert::same(1, $books->countStored());

$books = $tag->books->toCollection()->findBy([
'author->tagFollowers->author->id' => 1,
]);
Assert::same(1, $books->countStored());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ class RelationshipOneHasManyTest extends DataTestCase

Assert::same(1, \count($author->books));
}


public function testCountStoredOnOneHasManyRelationshipCondition()
{
$publisher = $this->orm->publishers->getByIdChecked(1);
$books = $publisher->books->toCollection()->findBy([
'tags->id' => 1,
]);
Assert::same(1, $books->countStored());

$books = $publisher->books->toCollection()->findBy([
ICollection::OR,
'title' => 'Book 1',
'tags->id' => 1,
]);
Assert::same(1, $books->countStored());
}
}


Expand Down

0 comments on commit 1fc1c9c

Please sign in to comment.