Skip to content

Fix api-platform/core #1246: configure default order on ApiResource annotation #235

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 3 commits into from
Sep 11, 2017

Conversation

vincentchalamon
Copy link
Contributor

No description provided.

@@ -0,0 +1,37 @@
# Override default order
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about naming the file order.md or ordering.md and merging everything related to ordering (the global config option, the filter and this config option) in this file?

It will be easier to understand for a new comer IMP.

Btw the title must be in Title Case (Override Default Order)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't explain this default order in the order filter cause it's a different feature. I better think we should mention it in the order filter and in the global config option, but keep it separated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO having a specific doc for all ordering features would be better in term of understanding for a newcomer, it was my point.

@@ -0,0 +1,37 @@
# Override default order

API Platform Core provides an easy way to override default order in your collection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of the collections?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of Core too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of the collections

Why plural?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took Core keyword from many .md files in this project. If we get rid of it, please open a new PR to remove it everywhere.


API Platform Core provides an easy way to override default order in your collection.

By default, it will order by resource identifier(s) using ASC direction. If you want to customize this order, you must add an `order` attribute on your ApiResource annotation:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, it will order by resource identifier(s) using ASC direction.

IIRC, it's not true. By default don't change the order of the underlying DBMS, and it is not defined nor consistent: https://dba.stackexchange.com/a/6053

@ApiResource annotation. Maybe it is worth mentioning that it can also be configured using XML and YAML.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Bridge/Doctrine/Orm/Extension/OrderExtension default order collection by identifier(s): https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Extension/OrderExtension.php#L56

Copy link
Member

@dunglas dunglas Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it's a bit more complicated. It's ordered by default only if you use the Symfony bridge (https://github.com/api-platform/core/blob/master/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php#L92), if you use the class directly, $this->order is null and the collection isn't ordered: https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Extension/OrderExtension.php#L54

But ok to keep it as is.

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(attributes={"order"={"foo", "ASC"}})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas Could it be interesting to configure a default order by an association, like author.username?


/**
* ...
* @ApiProperty()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation is useless, you can remove it.

}
```

It's also possible to configure the default filter on an association property:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this part? So we can merge your PR. We will add it when the api-platform/core#1324 will be merged.


API Platform Core provides an easy way to override default order in your collection.

By default, it will order by resource identifier(s) using ASC direction. If you want to customize this order, you must add an `order` attribute on your ApiResource annotation:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, items in the collection are ordered in ascending (ASC) order by their resource identifier(s).

@@ -0,0 +1,88 @@
# Override Default Order

API Platform Core provides an easy way to override default order in your collection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... override the default order of items in your collection.

@meyerbaptiste meyerbaptiste force-pushed the issues/1246 branch 2 times, most recently from c0f56b3 to 3207dfd Compare September 11, 2017 12:54
@meyerbaptiste meyerbaptiste merged commit 085869b into api-platform:master Sep 11, 2017
@vincentchalamon vincentchalamon deleted the issues/1246 branch September 11, 2017 12:59
@meyerbaptiste
Copy link
Member

Thanks @vincentchalamon, don't forget to add the removed paragraph when api-platform/core#1324 will be merged:

It's also possible to configure the default filter on an association property:

<?php

// src/AppBundle/Entity/Book.php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
 * @ApiResource(attributes={"order"={"author.username"}})
 */
class Book
{
    // ...

    /**
     * @var User
     */
    public $author;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants