yiisoft / data

Data providers
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
45 stars 17 forks source link

Set default `$currentOrder` in `Sort` class #212

Open Tigrov opened 4 days ago

Tigrov commented 4 days ago

$currentOrder can be set to default value after normalizing config.

https://github.com/yiisoft/data/blob/15ddd6e3bbb58bc7e341e173b0b66a51936dcecc/src/Reader/Sort.php#L114-L126

This will help avoid an exception if $currentOrder is not set manually.

https://github.com/yiisoft/data/blob/15ddd6e3bbb58bc7e341e173b0b66a51936dcecc/src/Paginator/KeysetPaginator.php#L444-L446

vjik commented 3 days ago

How to understand which order is default?

Tigrov commented 3 days ago

Usually it's in ascending order.

vjik commented 3 days ago

Order in Sort object is field name + order direction, e. g. "id asc". Configuration may contains a lot of fields, how we can understand which field should use by default?

Tigrov commented 2 days ago

By default sort by all fields in ascending order

vjik commented 2 days ago

For set default order we should select field also, not only direction. How we can understand which field should use by default?

For example, configuration: ['id' => ..., 'name' => ..., 'create_date' => ...]. Order may be id asc or create_date asc or name asc ...

Tigrov commented 2 days ago

If we use KeysetPaginator, then sorting is required. In case $currentOrder is not set, we can use the first field from the configuration. If multiple fields are required for sorting, the user must initialize $currentOrder.