3

I am using SugarORM for my Android application. In my project I have couple of tables and I was wondering is there a way to to join them into another Class Object that has columns from multiple tables?

If yes, then example would be very helpful.

MrJonas
  • 45
  • 4

1 Answers1

7

SugarORM provides a Query Builder object for simple queries. Since it doesn't provide supporto for joins, you can directly execute a raw query and store the result into an object created ad-hoc.

So, build your custom raw query renaming the fields in the SELECT part

CustomOBJ.executeQuery("SELECT tableA.fieldA as field1, tableA.fieldB as field2, tableB.fieldA as field 3 FROM tableA JOIN tableB WHERE .....");

and then create your custom object

public CustomOBJ {
    private String field1;
    private String field2;  
    private String field3;

   public CustomOBJ(){} //you must declare an empty constructor

   //getters
   //setters
}

Here the documentation:

Soria Gustavo
  • 519
  • 4
  • 9
  • 1
    If you're using a `SELECT` query, you should use the `findWithQuery` method, as it returns a list, opposed to `executeQuery`, which is `void`. (Using SugarORM v1.5) – FirstOne Jun 22 '18 at 19:22
  • 1
    So, following the answer, it should be `CustomOBJ.findWithQuery(CustomOBJ.class, "SELECT ...", ...)` - replace and data with your own. (And as a sidenote, you can use `SugarRecord.findWithQuery` if it feels less weird than repeating the object). – FirstOne Jun 22 '18 at 19:25
  • Thanks for the link! It helped me. – V.March Dec 13 '18 at 15:15