Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SugarOrm: OrderBy related table

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

like image 266
Lukesoft Avatar asked Sep 14 '15 12:09

Lukesoft


2 Answers

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)

like image 164
Soria Gustavo Avatar answered Oct 17 '22 02:10

Soria Gustavo


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);
like image 3
DeanK Avatar answered Oct 17 '22 00:10

DeanK