Skip to content

Commit

Permalink
Merge pull request #727 from nextras/ordering-has-many
Browse files Browse the repository at this point in the history
Fix ordering
  • Loading branch information
hrach authored Jan 29, 2025
2 parents 08e3789 + 9f3f98f commit af22260
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/collection-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ return new DbalExpressionResult('SUBSTRING(%ex, 0, %i) = %s', [$expression->args

The implementation is different to Dbal's interface, because the filtering happens directly in PHP runtime. The method takes `ArrayCollectionHelper` instance for easier expression processing, `IEntity` instance to check if the expressions requires it (not) to be filtered out, and user input/function parameters.

Array collection function returns `ArrayExpressionResult` instance wrapping a mixed value, the kind (type) of the wrapped value depends on which context it will be user or evaluated later. Ultimately, the value will be interpreted as a boolean to indicate if the entity should be filtered out in the `ICollection::findBy()` call; alternatively, the value will be used for comparison (`<=>`) of two entities when used for `ICollection::oderBy()` call.
Array collection function returns `ArrayExpressionResult` instance wrapping a mixed value, the kind (type) of the wrapped value depends on which context it will be user or evaluated later. Ultimately, the value will be interpreted as a boolean to indicate if the entity should be filtered out in the `ICollection::findBy()` call; alternatively, the value will be used for comparison (`<=>`) of two entities when used for `ICollection::orderBy()` call.

Let's see an example: a "Like" collection function; We want to compare a property expression to the passed user-input value using a prefix comparison.

Expand Down
1 change: 1 addition & 0 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function orderBy($expression, string $direction = ICollection::ASC): ICol
public function resetOrderBy(): ICollection
{
$collection = clone $this;
$collection->ordering = [];
$collection->getQueryBuilder()->orderBy(null);
return $collection;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/integration/Collection/collection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ class CollectionTest extends DataTestCase
}


public function testOrderingReset(): void
{
$books = $this->orm->books->findAll()
->orderBy('author->name')
->resetOrderBy()
->orderBy('author->name', ICollection::DESC)
->orderBy('id', ICollection::DESC)
->fetchPairs(null, 'id');

Assert::same([4, 3, 2, 1], $books);
}


public function testEmptyArray(): void
{
$books = $this->orm->books->findBy(['id' => []]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
"books".*
FROM
"books" AS "books"
LEFT JOIN "public"."authors" AS "author" ON (
"books"."author_id" = "author"."id"
)
ORDER BY
"author"."name" DESC,
"books"."id" DESC;

0 comments on commit af22260

Please sign in to comment.