6

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

1
  • Please mark your question as resolved if the below answer solved your issue. Thank you.
    – Ralphilius
    May 13, 2015 at 11:12

1 Answer 1

16

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.