Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm.io - Is it possible to find object by its subobject?

Tags:

android

realm

Doctor includes object Organization sub object:

@PrimaryKey
private int doctorId;
private FullName fullName;
private Age age;
private Organization organization;
private Position position;
private String category;
private String loyalty;
private List<Specialization> specializations;
private Contacts contacts;

Organization model has following parameters:

    @PrimaryKey
    private OrganizationId organizationId;
    private String organizationName;
    private String key;
//    private Address address;
    private String address;
    private String phoneNumber;

Filling values like this:

Organization organization = realm.createObject(Organization.class); // Create a new object
OrganizationId organizationId = realm.createObject(OrganizationId.class);
organizationId.setAggregateId("1");
organization.setOrganizationId(organizationId);
organization.setOrganizationName("1-я Клиника Ташкентской Медицинской Академии");
organization.setAddress("Адрес: г. Ташкент, ул. Фароби, 2");
organization.setPhoneNumber("Тел.: (+99871) 214-51-01, 214-50-86, 214-50-43");
organization.setKey(organization.getOrganizationName().toLowerCase());

Doctor doctor = realm.createObject(Doctor.class);
//FULL NAME
FullName fullName = realm.createObject(FullName.class);
fullName.setFirstName("Joe");
fullName.setLastName("Richard");
fullName.setMiddleName("Brown");
doctor.setFullName(fullName);
//CONTACTS
Contacts contacts = realm.createObject(Contacts.class);
String[] phoneNumbers = {"+998903735173"};
contacts.setPhoneNumbers(phoneNumbers);
doctor.setContacts(contacts);
//ORGANIZATION
doctor.setOrganization(organization);

For example, this code returns all doctors with A category:

RealmQuery<Doctor> query = realm.where(Doctor.class);
RealmResults<Doctor> rDoctors = query.contains("category", "A").findAll();
return rDoctors;

My app logic like this: first of all, I open list of organizations. When User clicks on one organization. This will open list of doctors.

So my question is can I find doctors by its sub object(Organization)? Something like this

RealmQuery<Doctor> query = realm.where(Doctor.class);
    RealmResults<Doctor> rDoctors = query.someMagicalMethod("organization", organization1).findAll();
    return rDoctors;

PS. Yes, I can get it by going deep into organization. I was wondering does Realm.io makes search by object possible. Anyways I love Realm.io

like image 479
Joe Rakhimov Avatar asked May 08 '15 12:05

Joe Rakhimov


1 Answers

I think it i possible. You can check it out here: http://realm.io/docs/java/latest/#link-queries

As per your case, you can try my following code:

RealmResults<Doctor> rDoctors = realm.where(Doctor.class)
                                .equalsTo("organization.organizationId", organizationId)
                                .findAll();
return rDoctors;

Please let me know if it works for you.

like image 57
Ralphilius Avatar answered Oct 25 '22 23:10

Ralphilius