@@ -30,26 +30,26 @@ public function findByList(int $listId, string $sortBy = 'custom'): array {
3030
3131 if ($ sortBy === 'category ' ) {
3232 // Left-join the categories table so items with no category still appear.
33- // Uncategorized items are grouped separately (sort_uncategorized = 1)
34- // so they appear after all categorized items regardless of DB null ordering.
33+ // Uncategorized items are pushed to the end via a CASE expression in
34+ // ORDER BY (we can't SELECT it as an alias because the mapper would
35+ // try to set it as an entity attribute).
3536 $ categories = Application::tableName ('categories ' );
36- $ qb ->select ($ items . '.* ' )
37- ->selectAlias (
38- $ qb ->createFunction ('CASE WHEN ' . $ items . '.category_id IS NULL THEN 1 ELSE 0 END ' ),
39- 'sort_uncategorized ' ,
40- )
41- ->from ($ items )
37+ $ qb ->select ('i.* ' )
38+ ->from ($ items , 'i ' )
4239 ->leftJoin (
43- $ items ,
40+ ' i ' ,
4441 $ categories ,
4542 'c ' ,
46- $ qb ->expr ()->eq ($ items . '.category_id ' , 'c.id ' ),
43+ $ qb ->expr ()->eq ('i.category_id ' , 'c.id ' ),
44+ )
45+ ->where ($ qb ->expr ()->eq ('i.list_id ' , $ qb ->createNamedParameter ($ listId , IQueryBuilder::PARAM_INT )))
46+ ->orderBy (
47+ $ qb ->createFunction ('CASE WHEN i.category_id IS NULL THEN 1 ELSE 0 END ' ),
48+ 'ASC ' ,
4749 )
48- ->where ($ qb ->expr ()->eq ($ items . '.list_id ' , $ qb ->createNamedParameter ($ listId , IQueryBuilder::PARAM_INT )))
49- ->orderBy ('sort_uncategorized ' , 'ASC ' )
5050 ->addOrderBy ('c.name ' , 'ASC ' )
51- ->addOrderBy ($ items . ' .name ' , 'ASC ' )
52- ->addOrderBy ($ items . ' .created_at ' , 'ASC ' );
51+ ->addOrderBy (' i .name ' , 'ASC ' )
52+ ->addOrderBy (' i .created_at ' , 'ASC ' );
5353 return $ this ->findEntities ($ qb );
5454 }
5555
0 commit comments