8

I am quite new to Parse.

I have a database set up using this code:

var Class = Parse.Object.extend("Class");
var Team = Parse.Object.extend("Team");
var Student = Parse.Object.extend("Student");

var newClass = new Class();
newClass.set("name", className);
newClass.set("code", classCode);
newClass.set("creator", currentUser);
var classACL = new Parse.ACL(currentUser);
classACL.setPublicReadAccess(true);
newClass.setACL(classACL);

newClass.save();

for (var i = 0; i < teamNames.length; i++) {
    var team = new Team();
    team.set("name", teamNames[i]);
    var teamACL = new Parse.ACL(currentUser);
    teamACL.setPublicReadAccess(true);
    team.setACL(teamACL);

    team.save();

    for (var j = 0; j < studentNames[i].length; j++) {
        if (studentNames[i][j]) {
            var student = new Student();
            student.set("name", studentNames[i][j]);

            student.set("parent", team);

            student.save();
        }
    }

    team.set("parent", newClass);

    team.save();
}

newClass.save(null, {
    success: function(newClass) {
        //success
    },
    error: function(newClass, error) {
        //fail
    }
});

Here Class, Team, and Student are modeled as one-to-many relationships.

Now when a student signs up for the class using his or her own user account, the corresponding Student's user column is set to the current user.

Then I want to list all the classes whose creator OR one of its student's user column (if exists) equals to currentUser.

How do I create such a query referencing multiple classes in Parse (or how can I optimize the database so that such a query can be made as efficient as possible -- without having to create two separate queries?)

Any help is appreciated.

Clarification:

I knew that I could do an or query as described in Parse docs (I should have stated this in the question), however, my question is about doing so on relational data (defined by a pointer type property to parent). Here I need user be a property of a Student instance, which belongs to Team, and then to Class, and I'd like to filter only Class objects that has either its creator property or one of its grandchildren's (an instance of Student) user property equal to the currentUser, effectively listing only the classes that you created or are registered as a student.

8
  • Here I need user be a property of a Student instance, which belongs to Team, and then to Class, and I'd like to filter only Class objects that has either its creator property or one of its grandchildren's (an instance of Student) user property equal to the currentUser, effectively listing only the classes that you created or are registered as a student. You want to achieve 2 things in 1 query or in separate ones? Commented Nov 6, 2015 at 1:34
  • @kodingralph ideally in 1 query if possible. In the end I just need an array of Class objects that meets the above condition. Commented Nov 6, 2015 at 1:47
  • @kodingralph yes, Student holds a predefined list of student names that are presented when a student signs up, where he/she gets to choose from the select box. There I will need to link the selected Student to the actual User. Commented Nov 6, 2015 at 2:03
  • I don't see how a student is associated with the Class in your schema. So, it is impossible to retrieve the Class object that has currentUser associated with. Correct me if I miss anything. Commented Nov 6, 2015 at 2:05
  • @kodingralph so actually, let's ignore Team for now. Then there are many Student instances that share a common pointer (through its parent property) to the same Class. Does this help make it clear? Commented Nov 6, 2015 at 2:08

2 Answers 2

1
+50

Since the current database schema is having nested Pointers, there is no easy way to achieve this without adjusting it.


Database Schema

In Class class, add a Relation or Array field to contain references to Student/User objects. If you use User as object pointer, we wouldn't need to look up for Student at first.

Query

I assume that you have students as new Array field in Class class. students contains User objects.

  var user = Parse.User.current();
  var studentQuery = new Parse.Query(Class);
  var creatorQuery = new Parse.Query(Class);

  studentQuery.equalTo("students", user);
  creatorQuery.equalTo("creator", user);
  var query = Parse.Query.or(studentQuery, creatorQuery);
  query.find().then(function(objects){
      // Proceed with the results
  },function(error){
      // Handle error
  });
Sign up to request clarification or add additional context in comments.

Comments

1

Ok, what you want to do in an OR query with an internal subquery. One call to parse and you can filter the student properties using the subquery.

var studentQuery = new Parse.Query(Student);
studentQuery.equalTo("user", Parse.User.current());

var firstQuery = new Parse.Query(Class);
firstQuery.matchesQuery("student", studentQuery);

var secondQuery = new Parse.Query(Class);
secondQuery.equalTo("creator", Parse.User.current());

var mainQuery = Parse.Query.or(firstQuery, secondQuery);
mainQuery.find({
    success: function(results) {
        // results contains a list of Classes where the user is either the creator or the user property of the student (if available)
    },
    error: function(error) {
        // There was an error.
    }
});

2 Comments

I knew that I could do an or query as described in Parse docs (I should have stated this in the question), however, my question is about doing so on relational data (defined by a pointer type property to parent). Here I need user be a property of a Student instance, which belongs to Team, and then to Class, and I'd like to filter only Class objects that has either its creator property or one of its grandchildren's (an instance of Student) user property equal to the currentUser, effectively listing only the classes that you created or are registered as a student.
Ok I revised. I hope I understood the model structure but you should be able to use the same process for whatever you need.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.