diff --git a/src/Collection/DbalCollection.php b/src/Collection/DbalCollection.php index 90a34f52..5ab8c752 100644 --- a/src/Collection/DbalCollection.php +++ b/src/Collection/DbalCollection.php @@ -7,6 +7,7 @@ use Nextras\Dbal\IConnection; use Nextras\Dbal\Platforms\Data\Fqn; use Nextras\Dbal\Platforms\MySqlPlatform; +use Nextras\Dbal\Platforms\SqlServerPlatform; use Nextras\Dbal\QueryBuilder\QueryBuilder; use Nextras\Orm\Collection\Expression\ExpressionContext; use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult; @@ -349,7 +350,12 @@ protected function getIteratorCount(): int { if ($this->resultCount === null) { $builder = clone $this->getQueryBuilder(); - if (!$builder->hasLimitOffsetClause()) { + + if ($this->connection->getPlatform()->getName() === SqlServerPlatform::NAME) { + if (!$builder->hasLimitOffsetClause()) { + $builder->orderBy(null); + } + } else { $builder->orderBy(null); } diff --git a/tests/cases/integration/Collection/collection.phpt b/tests/cases/integration/Collection/collection.phpt index d9db66a0..418205b0 100644 --- a/tests/cases/integration/Collection/collection.phpt +++ b/tests/cases/integration/Collection/collection.phpt @@ -8,6 +8,10 @@ namespace NextrasTests\Orm\Integration\Collection; +use Nextras\Dbal\Connection; +use Nextras\Dbal\Drivers\Exception\DriverException; +use Nextras\Dbal\ILogger; +use Nextras\Dbal\Result\Result; use Nextras\Orm\Collection\ArrayCollection; use Nextras\Orm\Collection\DbalCollection; use Nextras\Orm\Collection\EmptyCollection; @@ -316,6 +320,14 @@ class CollectionTest extends DataTestCase } + public function testCountStoredDbalWithoutOrderByClause(): void + { + $collection = $this->orm->books->findAll()->orderBy('title'); + Assert::same(4, $collection->countStored()); + Assert::same(4, $collection->limitBy(10, 0)->countStored()); + } + + public function testToArrayCollection(): void { $c1 = $this->orm->authors->findAll(); diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimited.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimited.sql index d2c3b922..9941e4d9 100644 --- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimited.sql +++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimited.sql @@ -1,4 +1,4 @@ SELECT "books".* FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 1; SELECT "books".* FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 10; -SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 1) temp; -SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 10) temp; +SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 1 OFFSET 1) temp; +SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 1 OFFSET 10) temp; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimitedWithJoin.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimitedWithJoin.sql index f2a1c549..e47d88be 100644 --- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimitedWithJoin.sql +++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountOnLimitedWithJoin.sql @@ -11,8 +11,6 @@ FROM ) WHERE "author"."name" = 'Writer 1' - ORDER BY - "books"."id" ASC LIMIT 5 ) temp; @@ -31,8 +29,6 @@ FROM ) WHERE "tag"."name" = 'Tag 1' - ORDER BY - "tag_followers"."tag_id" ASC LIMIT 3 ) temp; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountStoredDbalWithoutOrderByClause.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountStoredDbalWithoutOrderByClause.sql new file mode 100644 index 00000000..15cc77e7 --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testCountStoredDbalWithoutOrderByClause.sql @@ -0,0 +1,2 @@ +SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books") temp; +SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 10 OFFSET 0) temp;