Skip to content

Configure default order #1246

@vincentchalamon

Description

@vincentchalamon

By default, API Platform order list by resource identifier. The only way to default order list by another field would be to create a custom extension, and if the filter is not defined in url apply a default order.

For example:

$request = $this->requestStack->getCurrentRequest();
--
if (null === $request || !$request->query->has('foo')) {
    // Default order
    $queryBuilder->orderBy($queryBuilder->getRootAliases()[0].'.bar ASC');

    return;
}

// Custom order from request
$queryBuilder->orderBy($queryBuilder->getRootAliases()[0].'.bar DESC');

But it could be interesting to configure it directly from the @ApiResource annotation declaration, like:

/**
 * @ApiResource(attributes={"order"={"bar"="DESC"}})
 * @ORM\Entity()
 */
class Foo
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column()
     */
    private $bar;
}

@api-platform/core-team do you think it could be interesting?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions