Skip to content
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

Sort entity by method #3303

Closed
yalagin opened this issue Dec 8, 2019 · 3 comments
Closed

Sort entity by method #3303

yalagin opened this issue Dec 8, 2019 · 3 comments

Comments

@yalagin
Copy link

yalagin commented Dec 8, 2019

API-platform sort entity only by arguments. it will be nice to sort entity by methods too
for example, I want to sort workouts in day of week order

@ApiResource(attributes={"order"={"DayOfWeekOrder": "ASC"}})
class Workout 
{
/**
     * @var string|null
     * it's Monday/Wensday ...
     * @ORM\Column(type="string", nullable=true)
     * @Assert\Choice(callback={"DayOfWeek", "toArray"})
     *  @Groups({"withWorkouts","workout"})
     */
    private $dayOfWeek;

    public function DayOfWeekOrder(){
        return DayOfWeek::ORDER[$this->dayOfWeek];
    }
}

class DayOfWeek extends Enum
{
    public const ORDER = [
        self::MONDAY => 1,
        self::TUESDAY => 2,
        self::WEDNESDAY => 3,
        self::THURSDAY => 4,
        self::FRIDAY => 5,
        self::SATURDAY => 6,
        self::SUNDAY => 7,
        ];

    public const FRIDAY = 'Friday';
    public const MONDAY = 'Monday';
//and so on 
}

for now I am doing it in front-end

@teohhanhui
Copy link
Contributor

teohhanhui commented Dec 17, 2019

I'm not sure what you mean. Where do we sort by arguments?

@yalagin
Copy link
Author

yalagin commented Dec 18, 2019

@teohhanhui we have entity workout with string property dayOfWeek
workouts: [{id : 1, dayOfWeek : Moday},{id :2, dayOfWeek : Friday}]
if I want to sort days of the week in particular order form Monday to Sunday (or from today to the rest of the week )

it will nice to add to an entity some method that returns the number that we assign to the string and make sorting by it.

and we can just simply add to the docs of entity
@ApiResource(attributes={"order"={"DayOfWeekOrder": "ASC"}})
like I described above

@soyuka
Copy link
Member

soyuka commented Oct 22, 2021

#2254

@soyuka soyuka closed this as completed Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants