Skip to content

Commit fc822b3

Browse files
vincentchalamondunglas
authored andcommitted
Issues/1246 (#1323)
* Fix #1246 after merge * Fix #1246: add default order, allow multiple orders with optional direction
1 parent f6ccfff commit fc822b3

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ public function thereIsDummyObjects($nb)
139139
public function thereAreFooObjectsWithFakeNames($nb)
140140
{
141141
$names = ['Hawsepipe', 'Sthenelus', 'Ephesian', 'Separativeness', 'Balbo'];
142+
$bars = ['Lorem', 'Dolor', 'Dolor', 'Sit', 'Amet'];
142143

143144
for ($i = 0; $i < $nb; ++$i) {
144145
$foo = new Foo();
145146
$foo->setName($names[$i]);
147+
$foo->setBar($bars[$i]);
146148

147149
$this->manager->persist($foo);
148150
}

features/main/default_order.feature

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,40 @@ Feature: Default order
1717
"@id": "/foos",
1818
"@type": "hydra:Collection",
1919
"hydra:member": [
20+
{
21+
"@id": "/foos/5",
22+
"@type": "Foo",
23+
"id": 5,
24+
"name": "Balbo",
25+
"bar": "Amet"
26+
},
2027
{
2128
"@id": "/foos/2",
2229
"@type": "Foo",
2330
"id": 2,
24-
"name": "Sthenelus"
31+
"name": "Sthenelus",
32+
"bar": "Dolor"
2533
},
2634
{
27-
"@id": "/foos/4",
35+
"@id": "/foos/3",
2836
"@type": "Foo",
29-
"id": 4,
30-
"name": "Separativeness"
37+
"id": 3,
38+
"name": "Ephesian",
39+
"bar": "Dolor"
3140
},
3241
{
3342
"@id": "/foos/1",
3443
"@type": "Foo",
3544
"id": 1,
36-
"name": "Hawsepipe"
45+
"name": "Hawsepipe",
46+
"bar": "Lorem"
3747
},
3848
{
39-
"@id": "/foos/3",
40-
"@type": "Foo",
41-
"id": 3,
42-
"name": "Ephesian"
43-
},
44-
{
45-
"@id": "/foos/5",
49+
"@id": "/foos/4",
4650
"@type": "Foo",
47-
"id": 5,
48-
"name": "Balbo"
51+
"id": 4,
52+
"name": "Separativeness",
53+
"bar": "Sit"
4954
}
5055
],
5156
"hydra:totalItems": 5,

src/Bridge/Doctrine/Orm/Extension/OrderExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
4545
if (null !== $this->resourceMetadataFactory) {
4646
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
4747
if (null !== $defaultOrder) {
48-
$queryBuilder->addOrderBy('o.'.$defaultOrder[0], $defaultOrder[1] ?? 'ASC');
48+
foreach ($defaultOrder as $field => $order) {
49+
if (is_int($field)) {
50+
$field = $order;
51+
$order = 'ASC';
52+
}
53+
$queryBuilder->addOrderBy('o.'.$field, $order);
54+
}
4955

5056
return;
5157
}

tests/Bridge/Doctrine/Orm/Extension/OrderExtensionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testApplyToCollectionWithOrderOverriden()
7575
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
7676
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
7777

78-
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadata(null, null, null, null, null, ['order' => ['foo', 'DESC']]));
78+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadata(null, null, null, null, null, ['order' => ['foo' => 'DESC']]));
7979

8080
$emProphecy = $this->prophesize(EntityManager::class);
8181
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
@@ -93,11 +93,12 @@ public function testApplyToCollectionWithOrderOverridenWithNoDirection()
9393
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
9494

9595
$queryBuilderProphecy->addOrderBy('o.foo', 'ASC')->shouldBeCalled();
96+
$queryBuilderProphecy->addOrderBy('o.bar', 'DESC')->shouldBeCalled();
9697

9798
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
9899
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
99100

100-
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadata(null, null, null, null, null, ['order' => ['foo']]));
101+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadata(null, null, null, null, null, ['order' => ['foo', 'bar' => 'DESC']]));
101102

102103
$emProphecy = $this->prophesize(EntityManager::class);
103104
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());

tests/Fixtures/TestBundle/Entity/Foo.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
1515

16-
use ApiPlatform\Core\Annotation\ApiProperty;
1716
use ApiPlatform\Core\Annotation\ApiResource;
1817
use Doctrine\ORM\Mapping as ORM;
1918
use Symfony\Component\Validator\Constraints as Assert;
@@ -24,7 +23,7 @@
2423
* @author Vincent Chalamon <[email protected]>
2524
*
2625
* @ApiResource(attributes={
27-
* "order"={"name", "DESC"}
26+
* "order"={"bar", "name": "DESC"}
2827
* })
2928
* @ORM\Entity
3029
*/
@@ -40,14 +39,21 @@ class Foo
4039
private $id;
4140

4241
/**
43-
* @var string The dummy name
42+
* @var string The foo name
4443
*
4544
* @ORM\Column
4645
* @Assert\NotBlank
47-
* @ApiProperty(iri="http://schema.org/name")
4846
*/
4947
private $name;
5048

49+
/**
50+
* @var string The foo bar
51+
*
52+
* @ORM\Column
53+
* @Assert\NotBlank
54+
*/
55+
private $bar;
56+
5157
public function getId()
5258
{
5359
return $this->id;
@@ -62,4 +68,14 @@ public function getName()
6268
{
6369
return $this->name;
6470
}
71+
72+
public function getBar()
73+
{
74+
return $this->bar;
75+
}
76+
77+
public function setBar($bar)
78+
{
79+
$this->bar = $bar;
80+
}
6581
}

0 commit comments

Comments
 (0)