Skip to content

How to query and include parent object in child query #357

Closed
@GursheeshSingh

Description

@GursheeshSingh

I have two classes -

PollLike and Poll

The PollLike is like child of the poll object

class Poll extends ParseObject implements ParseCloneable {
  Poll.clone(String className, PollType pollType) : this(className, pollType);

  @override
  clone(Map map) => Poll.clone(className, pollType)..fromJson(map);

  Poll(this.className, this.pollType) : super(className);

  String className;
  PollType pollType;

  String get question => get<String>("question");

  String get coverImage => get<String>("coverImage");

  String get optionsType => get<String>("optionsType");

  int get totalNumberOfVotes => get<int>("totalNumberOfVotes");

  int get numberOfLikes => get<int>("numberOfLikes");

  List<dynamic> get options => get<List<dynamic>>("options");

  List<dynamic> get tags => get<List<dynamic>>("tags");

  Map<String, dynamic> get votes => get<Map<String, dynamic>>("votes");

  ///Name of user
  String get userName => get<String>("userName");

  String get expiryDateTime => get<String>("expiryDateTime");

  set question(String question) => set<String>("question", question);

  set coverImage(String coverImage) => set<String>("coverImage", coverImage);

  set optionsType(String optionsType) =>
      set<String>("optionsType", optionsType);

  set totalNumberOfVotes(int totalNumberOfVotes) =>
      set<int>("totalNumberOfVotes", totalNumberOfVotes);

  set numberOfLikes(int numberOfLikes) =>
      set<int>("numberOfLikes", numberOfLikes);

  ///Name of user
  set userName(String userName) => set<String>("userName", userName);

  set expiryDateTime(String expiryDateTime) =>
      set<String>("expiryDateTime", expiryDateTime);

  set options(List<dynamic> options) => set<List<dynamic>>("options", options);

  set tags(List<dynamic> tags) => set<List<dynamic>>("tags", tags);

  set votes(Map<String, dynamic> votes) =>
      set<Map<String, dynamic>>("votes", votes);
}

class PollLike extends ParseObject implements ParseCloneable {
  PollLike.clone(String className, PollType pollType)
      : this(className, pollType);

  @override
  clone(Map map) => PollLike.clone(className, pollType)..fromJson(map);

  PollLike(this.className, this.pollType) : super(className);

  String className;
  PollType pollType;

  String get userId => get<String>("userId");

  String get pollId => get<String>("pollId");

  Poll get parent => get<Poll>("parent");

  set userId(String userId) => set<String>("userId", userId);

  set pollId(String pollId) => set<String>("pollId", pollId);

  set userName(String userName) => set<String>("userName", userName);

  set userProfilePicture(String userProfilePicture) =>
      set<String>("userProfilePicture", userProfilePicture);

  set parent(Poll parent) => set<Poll>("parent", parent);
}

Each class represents two classes in the Actual database.

Poll - OfficialPolls and UserPolls
PollLike - OfficialPollLikes and UserPollLikes

Now I save a OfficialPollLike with an official poll -

Screenshot 2020-04-17 at 4 56 50 PM

OfficialPollLike stores a pointer to the OfficialPoll object

Now, I want to retrieve OfficialPollLikes of a particular user with the Poll details already included in the response-

  QueryBuilder queryBuilder =
      QueryBuilder(PollLike("OfficialPollLikes", PollType.OFFICIAL))
        ..orderByDescending("_created_at")
        ..setLimit(PollLikesProvider.LIMIT)
        ..includeObject(["parent"]);

Here is the response-

flutter: {"className":"OfficialPollLikes","objectId":"T8wVTurJef","createdAt":"2020-04-17T11:17:12.026Z","updatedAt":"2020-04-17T11:17:12.026Z","userId":"3g3lk4M6Xl","pollId":"5e96e1d47b37522678375591","parent":{"__type":"Pointer","className":"OfficialPolls","objectId":"5e96e1d47b37522678375591"}}
flutter: {"className":"OfficialPollLikes","objectId":"AO5g5CAJFx","createdAt":"2020-04-17T11:17:08.647Z","updatedAt":"2020-04-17T11:17:08.647Z","userId":"3g3lk4M6Xl","pollId":"5e96e1e57b37522678375592","parent":{"__type":"Pointer","className":"OfficialPolls","objectId":"5e96e1e57b37522678375592"}}

It does not contain any fields of the Poll class except the pointer.
I am also new to Pointers and Relation on Parse SDK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions