Skip to content

Commit

Permalink
Merge pull request #64 from baileylo/arraycollection-matching-method-…
Browse files Browse the repository at this point in the history
…multiple-ordering-fix

Arraycollection matching method multiple ordering fix
  • Loading branch information
deeky666 committed Sep 11, 2015
2 parents aacbad2 + 8314d60 commit 866e100
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Doctrine/Common/Collections/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ public function matching(Criteria $criteria)
}

if ($orderings = $criteria->getOrderings()) {
$next = null;
foreach (array_reverse($orderings) as $field => $ordering) {
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
}

uasort($filtered, $next);
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,26 @@ public function testMatchingWithSortingPreservesyKeys()
->toArray()
);
}

public function testMultiColumnSortAppliesAllSorts()
{
$collection = new ArrayCollection(array(
array('foo' => 1, 'bar' => 2),
array('foo' => 2, 'bar' => 4),
array('foo' => 2, 'bar' => 3)
));

$expected = array(
1 => array('foo' => 2, 'bar' => 4),
2 => array('foo' => 2, 'bar' => 3),
0 => array('foo' => 1, 'bar' => 2)
);

$this->assertSame(
$expected,
$collection
->matching(new Criteria(null, array('foo' => Criteria::DESC, 'bar' => Criteria::DESC)))
->toArray()
);
}
}

0 comments on commit 866e100

Please sign in to comment.