SugarOrm: OrderBy related table
Asked Answered
S

2

7

With SugarORM , I understand in a relationship I can do this

public class Book extends SugarRecord<Book> {
  String name;
  String ISBN;
  String title;
  String shortSummary;

  // defining a relationship
  Author author;
}

How then can i do a find on Book.class such that i can order by authors.

I have tried

Book.find(Book.class, null, null, null, "author.id desc",null);;

and

Book.find(Book.class, null, null, null, "author desc",null);;

and all these wont work

Suckle answered 14/9, 2015 at 12:42 Comment(0)
L
10

For a simple query it is recommended to use the query builder approach, for example:

Select.from(Book.class)
.orderBy("ISBN")
.list();

Anyway remember that SugarORM is based on SQLite and in your specific case you are trying to order by a relationship. Then you should build a custom query with a join, and the order by a field of the table Author (id, name, surname, etc...depending on your purpose)

Leviathan answered 11/2, 2016 at 8:15 Comment(3)
can set orderBy descending and ascending with default method?Mush
actually it supports the ASC method by default, and it seems there is no way to choose between ASC and DESC. For the while if you need DESC you should use a raw queryLeviathan
@SoriaGustavo If you wish to specify asc / desc, just put it within the orderBy, e.g. orderBy("ISBN desc").Bounteous
V
3

My suggestion is that you use Sugar ORM Custom Query Builder, which can be called by using this

Book.executeQuery("VACUUM");

Or you can also use method findWithQuery with the following syntax

List<Note> notes = Note.findWithQuery(Note.class, "SELECT * FROM Book 
                                        ORDER BY author DESC", null);
Vicegerent answered 23/9, 2015 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.