diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs index bada35623b7f..5c7488401f5b 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs @@ -152,20 +152,15 @@ protected override string ApplySystemOrdering(ref Sql sql, Ordering 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(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 joins = Sql() .InnerJoin("ctype").On( (content, contentType) => content.ContentTypeId == contentType.NodeId, aliasRight: "ctype") @@ -185,9 +180,9 @@ protected override string ApplySystemOrdering(ref Sql sql, Ordering // 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(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);