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 ambiguous column in search with ORDER BY #15899

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6477,7 +6477,10 @@ protected function prepareOrderBy($sort, $sortOrder) {
// Pretty sure this validation ALSO happens in the order clause & this can't be reached but...
// this might give some early warning.
CRM_Utils_Type::validate($fieldIDsInOrder, 'CommaSeparatedIntegers');
$order = str_replace("$field", "field({$fieldSpec['name']},$fieldIDsInOrder)", $order);
// use where if it's set to fully qualify ambiguous column names
// i.e. civicrm_contribution.contribution_status_id instead of contribution_status_id
$pseudoColumnName = $fieldSpec['where'] ?? $fieldSpec['name'];
$order = str_replace("$field", "field($pseudoColumnName,$fieldIDsInOrder)", $order);
}
//CRM-12565 add "`" around $field if it is a pseudo constant
// This appears to be for 'special' fields like locations with appended numbers or hyphens .. maybe.
Expand Down