Skip to content

Issues/1246 #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ public function thereIsDummyObjects($nb)
public function thereAreFooObjectsWithFakeNames($nb)
{
$names = ['Hawsepipe', 'Sthenelus', 'Ephesian', 'Separativeness', 'Balbo'];
$bars = ['Lorem', 'Dolor', 'Dolor', 'Sit', 'Amet'];

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

$this->manager->persist($foo);
}
Expand Down
33 changes: 19 additions & 14 deletions features/main/default_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,40 @@ Feature: Default order
"@id": "/foos",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/foos/5",
"@type": "Foo",
"id": 5,
"name": "Balbo",
"bar": "Amet"
},
{
"@id": "/foos/2",
"@type": "Foo",
"id": 2,
"name": "Sthenelus"
"name": "Sthenelus",
"bar": "Dolor"
},
{
"@id": "/foos/4",
"@id": "/foos/3",
"@type": "Foo",
"id": 4,
"name": "Separativeness"
"id": 3,
"name": "Ephesian",
"bar": "Dolor"
},
{
"@id": "/foos/1",
"@type": "Foo",
"id": 1,
"name": "Hawsepipe"
"name": "Hawsepipe",
"bar": "Lorem"
},
{
"@id": "/foos/3",
"@type": "Foo",
"id": 3,
"name": "Ephesian"
},
{
"@id": "/foos/5",
"@id": "/foos/4",
"@type": "Foo",
"id": 5,
"name": "Balbo"
"id": 4,
"name": "Separativeness",
"bar": "Sit"
}
],
"hydra:totalItems": 5,
Expand Down
8 changes: 7 additions & 1 deletion src/Bridge/Doctrine/Orm/Extension/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
if (null !== $this->resourceMetadataFactory) {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
if (null !== $defaultOrder) {
$queryBuilder->addOrderBy('o.'.$defaultOrder[0], $defaultOrder[1] ?? 'ASC');
foreach ($defaultOrder as $field => $order) {
if (is_int($field)) {
$field = $order;
$order = 'ASC';
}
$queryBuilder->addOrderBy('o.'.$field, $order);
}

return;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Bridge/Doctrine/Orm/Extension/OrderExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testApplyToCollectionWithOrderOverriden()
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);

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

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

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

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

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

$emProphecy = $this->prophesize(EntityManager::class);
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
Expand Down
24 changes: 20 additions & 4 deletions tests/Fixtures/TestBundle/Entity/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
Expand All @@ -24,7 +23,7 @@
* @author Vincent Chalamon <[email protected]>
*
* @ApiResource(attributes={
* "order"={"name", "DESC"}
* "order"={"bar", "name": "DESC"}
* })
* @ORM\Entity
*/
Expand All @@ -40,14 +39,21 @@ class Foo
private $id;

/**
* @var string The dummy name
* @var string The foo name
*
* @ORM\Column
* @Assert\NotBlank
* @ApiProperty(iri="http://schema.org/name")
*/
private $name;

/**
* @var string The foo bar
*
* @ORM\Column
* @Assert\NotBlank
*/
private $bar;

public function getId()
{
return $this->id;
Expand All @@ -62,4 +68,14 @@ public function getName()
{
return $this->name;
}

public function getBar()
{
return $this->bar;
}

public function setBar($bar)
{
$this->bar = $bar;
}
}