data icon indicating copy to clipboard operation
data copied to clipboard

Set default `$currentOrder` in `Sort` class

Open Tigrov opened this issue 1 year ago • 7 comments

$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

Tigrov avatar Nov 27 '24 05:11 Tigrov

How to understand which order is default?

vjik avatar Nov 27 '24 11:11 vjik

Usually it's in ascending order.

Tigrov avatar Nov 27 '24 11:11 Tigrov

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?

vjik avatar Nov 27 '24 11:11 vjik

By default sort by all fields in ascending order

Tigrov avatar Nov 28 '24 06:11 Tigrov

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 ...

vjik avatar Nov 28 '24 07:11 vjik

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.

Tigrov avatar Nov 28 '24 10:11 Tigrov

Makes sense.

samdark avatar Dec 06 '24 06:12 samdark