V11: Fix ordering by published in list of content (#13474)

* Update sort statement to use published field for invariant content

* Order by published if invariant
This commit is contained in:
Mole
2022-11-25 11:37:46 +01:00
committed by GitHub
parent 02275ed772
commit 37b33641c3

View File

@@ -152,20 +152,15 @@ public class DocumentRepository : ContentRepositoryBase<int, IContent, DocumentR
if (ordering.OrderBy.InvariantEquals("published"))
{
// no culture = can only work on the global 'published' flag
// no culture, assume invariant and simply order by published.
if (ordering.Culture.IsNullOrWhiteSpace())
{
// see notes in ApplyOrdering: the field MUST be selected + aliased, and we cannot have
// the whole CASE fragment in ORDER BY due to it not being detected by NPoco
sql = Sql(InsertBefore(sql, "FROM", ", (CASE WHEN pcv.id IS NULL THEN 0 ELSE 1 END) AS ordering "),
sql.Arguments);
return "ordering";
return SqlSyntax.GetFieldName<DocumentDto>(x => x.Published);
}
// invariant: left join will yield NULL and we must use pcv to determine published
// variant: left join may yield NULL or something, and that determines published
Sql<ISqlContext> joins = Sql()
.InnerJoin<ContentTypeDto>("ctype").On<ContentDto, ContentTypeDto>(
(content, contentType) => content.ContentTypeId == contentType.NodeId, aliasRight: "ctype")
@@ -185,9 +180,9 @@ public class DocumentRepository : ContentRepositoryBase<int, IContent, DocumentR
// the whole CASE fragment in ORDER BY due to it not being detected by NPoco
var sqlText = InsertBefore(sql.SQL, "FROM",
// when invariant, ie 'variations' does not have the culture flag (value 1), use the global 'published' flag on pcv.id,
// when invariant, ie 'variations' does not have the culture flag (value 1), it should be safe to simply use the published flag on umbracoDocument,
// otherwise check if there's a version culture variation for the lang, via ccv.id
", (CASE WHEN (ctype.variations & 1) = 0 THEN (CASE WHEN pcv.id IS NULL THEN 0 ELSE 1 END) ELSE (CASE WHEN ccvp.id IS NULL THEN 0 ELSE 1 END) END) AS ordering "); // trailing space is important!
$", (CASE WHEN (ctype.variations & 1) = 0 THEN ({SqlSyntax.GetFieldName<DocumentDto>(x => x.Published)}) ELSE (CASE WHEN ccvp.id IS NULL THEN 0 ELSE 1 END) END) AS ordering "); // trailing space is important!
sql = Sql(sqlText, sql.Arguments);