From 8ac61d4a38ae0fbf88b351dfbd1eb92adfd91135 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Mon, 6 Apr 2020 18:40:17 +0200 Subject: [PATCH] relationships: fixed counting entities when multiplied by joins [closes #393] --- .../Dbal/RelationshipMapperManyHasMany.php | 2 +- .../Dbal/RelationshipMapperOneHasMany.php | 2 +- .../relationships.manyHasMany.phpt | 15 +++++++++++++++ .../Relationships/relationships.oneHasMany.phpt | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Mapper/Dbal/RelationshipMapperManyHasMany.php b/src/Mapper/Dbal/RelationshipMapperManyHasMany.php index 8285c641..0c833cb9 100644 --- a/src/Mapper/Dbal/RelationshipMapperManyHasMany.php +++ b/src/Mapper/Dbal/RelationshipMapperManyHasMany.php @@ -217,7 +217,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"); diff --git a/src/Mapper/Dbal/RelationshipMapperOneHasMany.php b/src/Mapper/Dbal/RelationshipMapperOneHasMany.php index 714b88b7..0418c29f 100644 --- a/src/Mapper/Dbal/RelationshipMapperOneHasMany.php +++ b/src/Mapper/Dbal/RelationshipMapperOneHasMany.php @@ -229,7 +229,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()); diff --git a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt index a93a179e..c45452dc 100644 --- a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt @@ -277,6 +277,21 @@ class RelationshipManyHasManyTest extends DataTestCase Assert::same(1, \count($tag->books)); } + + + public function testCountStoredOnManyHasManyRelationshipCondition() + { + $tag = $this->orm->tags->getById(1); + $books = $tag->books->get()->findBy([ + 'this->author->id' => 1, + ]); + Assert::same(1, $books->countStored()); + + $books = $tag->books->get()->findBy([ + 'this->author->tagFollowers->author->id' => 1, + ]); + Assert::same(1, $books->countStored()); + } } diff --git a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt index 0d919b76..264ab51f 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt @@ -284,6 +284,23 @@ class RelationshipOneHasManyTest extends DataTestCase Assert::same(1, \count($author->books)); } + + + public function testCountStoredOnOneHasManyRelationshipCondition() + { + $publisher = $this->orm->publishers->getById(1); + $books = $publisher->books->get()->findBy([ + 'this->tags->id' => 1, + ]); + Assert::same(1, $books->countStored()); + + $books = $publisher->books->get()->findBy([ + ICollection::OR, + 'this->title' => 'Book 1', + 'this->tags->id' => 1, + ]); + Assert::same(1, $books->countStored()); + } }