RushOrm icon indicating copy to clipboard operation
RushOrm copied to clipboard

Doubt: Custom id and set relations

Open LimitTech opened this issue 9 years ago • 5 comments

Hello!

I have these classes:

public class Smartphone extends RushObject {
    private int id; // could be Long, not problem
    private Position position;
   // extra fields
}

public class Position extends RushObject {
    private int id; // could be Long, not problem
   // extra fields
}

I'm getting smartphones with position from an API REST (Laravel) using Retrofit:

public interface SmartphoneAPI {
    @GET("/api/smartphone")
    void get(Callback<List<Smartphone>> callback);
}

And then I do:

RushCore.getInstance().deleteAll(Smartphone.class);
RushCore.getInstance().save(smartphones);

Example:

[
    {
        id: 3,
        position_id:    156987,
        position: Object  // with the same fields as Position class
    }
]

Is there any way that I can relate the Position field of an Smartphone at an instance of Position? If I can also keep position id it would be better.

Thank you!

LimitTech avatar Jan 13 '16 15:01 LimitTech

Hi,

Yes there is. What you need to do every time before saving any Position object is register its id so that when its is saved it knows to override any exciting object in the db with that id.

Example before saving positions.

for(Position position : positions) {
    RushCore.getInstance().registerObjectWithId(position, position.getPositionId());
}
RushCore.getInstance().save(positions);

Example of saving Smartphones

for(Smartphon smartphone : smartphones) {
    RushCore.getInstance().registerObjectWithId(smartphone.getPosition(), smartphone.getPosition().getPositionId());
}
RushCore.getInstance().save(smartphones);

Hope that helps Thanks

Stuart-campbell avatar Feb 12 '16 11:02 Stuart-campbell

I don't know why but some registers aren't saved unless I explicitly save them However, good enough for me. Thank you.

LimitTech avatar Jan 24 '17 12:01 LimitTech

Aha!

Some relations aren't saved. I checked the query and it isn't trying to save them.

I found what was causing the problem but I don't know why. Maybe is something related to getMyId returns a String that can be cast to int?:

private int myid; // id from API

// When preparing to save the relation
public void myRegister() {
    RushCore.getInstance().registerObjectWithId(this, getMyId());
}

public String getMyId() {
// Not working        return String.valueOf(this.myid);
    return "_" + String.valueOf(this.myid); // Working! Just added a char
}

UPDATE:

I was wrong, the solution is to change the getMyId method for ONE model:

imagen

LimitTech avatar Jan 25 '17 08:01 LimitTech

That is really strange. I will try to lock it down in a unit test then can look at fixing it.

Stuart-campbell avatar Jan 25 '17 09:01 Stuart-campbell

I notice that the problem occurs when I want to register a list for a relation. It doesn't happen if I do model.save() in every iteration.

In example, a smartphone has a @RushList(classType = Position.class)

registerList(smartphone.positions):

private static <T extends Model> void registerList(List<T> models) {
    if (models.size() > 0) {
        for (T model : models) {
            model.myRegister();
        }
        RushCore.getInstance().save(models);
    }
 }

LimitTech avatar Jan 26 '17 15:01 LimitTech