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

FIX #19537 - List sorted by ref in adherents list #22907

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion htdocs/core/db/DoliDB.class.php
Expand Up @@ -247,7 +247,12 @@ public function order($sortfield = null, $sortorder = null)
if (!$return) $return .= ' ORDER BY ';
else $return .= ', ';

$return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
if (strpos($val, 'ref') !== false ){
$return .= "lpad(".preg_replace('/[^0-9a-z_\.]/i', '', $val).", 30, '0')";
Copy link
Member

Choose a reason for hiding this comment

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

Adding a lpad function in a sql search string breaks completely the ability of database to use index .
Also when a sort is done on ref it is done using natural_search so sort should already use a natural search 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.

Hi,
I understand that sorting with lpad is not efficient but I canot think to a better way of sorting this new field.

Currently, the natural search order may work when looking for something (meaning using the search parameter), but the sort order is wrongly displayed since it displays:
1 - adh.rowid_1
10 - adh.rowid_10
100 - adh.rowid_100
2 - adh.rowid_2
20 - adh.rowid_20
3 - adh.rowid_3
4 - adh.rowid_4

See discussion here :
https://www.dolibarr.fr/forum/t/tri-par-ref-anormal-dans-liste-adherents/41390

I can only see three ways to sort the list properly:

  1. with lpad, but this leads to a loss of efficiency
  2. sort on rowid, but it only works if string ref equals integer rowid
  3. add leading zeros when saving in the database (but can we do something so impactful in a bug fix?)

I'vd chose the first one cause it is not thé sort ordre by default, so the lpad fonction may not be call to often, but, for real it should be the second one witch is the best... but the ref field was created with the aim of no longer being equal to rowid so...

Copy link
Member

@eldy eldy Nov 28, 2022

Choose a reason for hiding this comment

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

If order give
1 - adh.rowid_1
10 - adh.rowid_10
100 - adh.rowid_100
2 - adh.rowid_2
20 - adh.rowid_20
3 - adh.rowid_3
4 - adh.rowid_4

It means result is an alphabetical search. We want a sort on string for ref, not a sort on numeric. Don't forget that the ref is a string and will be later generated by a numbering rule like other modules.
It is temporarily a numeric for the transition, but value will soon be MEMyymm-99999
There is no real solution for natural sorting using sql, but this need should disappear when ref numbering will be implemented.

$val='';
} else {
$return .= preg_replace('/[^0-9a-z_\.]/i', '', $val); // Add field
}

$tmpsortorder = (empty($orders[$i]) ? '' : trim($orders[$i]));

Expand Down