If we take a good look at theparseQuery.phpfile which located underapp/libraries/parse, we will notice that the class contains of twenty five functions:
• find function: Which prepare our parameters and return the result of executing the requestfunction which inherited fromparseclass.
• setCount function: We have used this function within our codes, and it will just add the
count⁴⁷parameter to our query, and this will return the total numbers of records we have
in Parse Data, and we can use it to minimize the numbers of requests we do to get the data from Parse.com.
• getCount function: This function will return the total numbers of records we have. • setLimit function: This function will be used to tell Parse.com how many records we want
to retrieve, but be careful since Parse.com does not allow more than 1000 records to return via each query.
• setSkip function: This function will be used to tell Parse.com how many records we want to skip before we get our records.
• orderBy function: This function will tell Parse.com which field we want to use to order ascending our retrieved records.
• orderByAscending function: Is identical to theorderByfunction.
• orderByDescending function: Is the opposite to the orderBy and orderByAscending functions, but they all work the same, by just send the field name as a parameter to the function.
⁴⁵https://github.com/apotropaic/parse.com-php-library/pulls ⁴⁶https://parse.com/docs/rest#objects
Mastering Parse Query Class 94 • whereInclude function: In parse, we saw that we can have fields asPointerwhich we can think of asForeign keyto the primary class, and in general if we query a class which has a pointer field to another class, we will get only the objectId, type and the class name of that field, but if we tell Parse.com that we need to include this field, we will get all the information, except it will work for only one level so you can’t get the full information of a pointers pointer fields.
1 $comments = new parseQuery('comments'); 2 $comments->setSkip(0);
3 $comments->setLimit(1); 4 $comments->setCount(true); 5 $result = $comments->find(); 6 return Response::json($result);
and the result will be : 1 { 2 "results": [ 3 { 4 "post": { 5 "__type": "Pointer", 6 "className": "posts", 7 "objectId": "J9mLUW0heO" 8 }, 9 "approved": false, 10 "authorEmail": "[email protected]", 11 "authorName": "jone doe",
12 "commentBody": "this is the comment of the year", 13 "createdAt": "2013-09-06T17:50:46.124Z", 14 "updatedAt": "2013-10-12T16:31:44.834Z", 15 "objectId": "lfZlfnfmTY" 16 } 17 ], 18 "count": 3 19 }
As we can see, since we didn’t tell the Parse.com that we need to include thepostpointers, we didn’t get much information, but if we just add thewhereIncludethe results will be different:
Mastering Parse Query Class 95 1 $comments = new parseQuery('comments');
2 $comments->setSkip(0);
3 $comments->whereInclude('post'); 4 $comments->setLimit(1);
5 $comments->setCount(true); 6 $result = $comments->find(); 7 return Response::json($result);
So lets see the result: 1 {
2 "results": [
3 {
4 "post": {
5 "active": true,
6 "body": "Contrary to popular belief, Lorem Ipsum is not simpl\ 7 y random text. It has roots in a piece of classical Latin literature from 45 \ 8 BC, making it over 2000 years old. Richard McClintock, a Latin professor at H\ 9 ampden-Sydney College in Virginia, looked up one of the more obscure Latin wo\ 10 rds, consectetur, from a Lorem Ipsum passage, and going through the cites of \ 11 the word in classical literature, discovered the undoubtable source. Lorem Ip\ 12 sum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Maloru\ 13 m\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is\ 14 a treatise on the theory of ethics, very popular during the Renaissance. The\ 15 first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a li\ 16 ne in section 1.10.32.\r\n\r\nThe standard chunk of Lorem Ipsum used since th\ 17 e 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.3\ 18 3 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in the\ 19 ir exact original form, accompanied by English versions from the 1914 transla\ 20 tion by H. Rackham.",
21 "title": "Where does Lorem Ipsum come from?", 22 "createdAt": "2013-09-06T17:49:11.938Z", 23 "updatedAt": "2013-11-24T05:41:39.590Z", 24 "objectId": "J9mLUW0heO", 25 "__type": "Object", 26 "className": "posts" 27 }, 28 "approved": false, 29 "authorEmail": "[email protected]", 30 "authorName": "jone doe",
31 "commentBody": "this is the comment of the year", 32 "createdAt": "2013-09-06T17:50:46.124Z",
33 "updatedAt": "2013-10-12T16:31:44.834Z", 34 "objectId": "lfZlfnfmTY"
35 }
Mastering Parse Query Class 96 37 "count": 3
38 }
Note:
nice thing about thewhereIncludefunction is that you can pass a string as parameter,
which will contain the names of the fields you want to include, separated by comma like:posts,users,someotherfield, and it will get all the information you asked for. • where and whereEqualTo functions: both functions do the same thing, they get the key
and the value as parameters, so we tell Parse.com that we need the record/records which a field (key) has the matching value.
• whereNotEqualTo function: Is the opposite of thewherefunction, so it will help us to exclude the records which has the field we specify, with the value we provide.
• whereGreaterThan function: We can use this function to get all the records which has the field we specify, with value greater than the value we provide.
• whereLessThan function: Is the opposite of thewhereGreaterThanfunction.
• whereGreaterThanOrEqualTo function: This function work the same aswhereGreaterThan except that it will also include the records which has the field we specify, with value also equal to the one we provide.
• whereLessThanOrEqualTo function: This will work the same as whereLessThanbut it will include also the records which has the field we specify, with value also equal to the one we provide.
• whereContainedIn function: This will help us to get all the records which has the value of the field we specify as one of the value we provide as array.
1 $games = new parseQuery('games');
2 $games->whereContainedIn('score',array(1,2,4)); 3 $result = $games->find();
• whereNotContainedIn function: Work as the opposite of the whereContainedIn func- tion.
• whereExists function: Some times we may have records with fields contains no data, for Parse.com it will be fields of undefinedvalue, so this function will make sure that we can get all the records we want which has value.
1 $games = new parseQuery('games'); 2 $games->whereExists('score'); 3 $result = $games->find();
• whereDoesNotExist function: Is the opposite of whereExistsfunction.
• whereRegex function: We can use this function to query the data based on regular expression pattern, but you should note that Parse.com start deprecating general regex queries that don’t match an exact prefix.
Mastering Parse Query Class 97 1 $games = new parseQuery('games');
2 // this will get all the games which has the name field
3 // start with `J`
4 $games->whereRegex('gameName',"^\QJ\E"); 5 $result = $games->find();
• wherePointer function: This is so useful when dealing with pointers, we didn’t use it but for example we can use it when querying the comments class for records belong to a specific post.
• whereInQuery function: If you want to retrieve objects where a field contains an object that matches another query, you can use the whereInQuery function. Note that the default limit of 100 and maximum limit of 1000 apply to the inner query as well, so with large data sets you may need to construct queries carefully to get the desired behavior. For example, imagine you have Post class and a Comment class, where each Comment has a relation to its parent Post. You can find comments on posts with images by doing:
1 $comments = new parseQuery('comments'); 2 $comments->whereInQuery('post','posts',array( 3 'where'=> array(
4 'image' => array('$exists' => true)
5 )
6 ));
7 $result = $comments->find(); 8 return Response::json($result);
I know its not that pretty but this is how to get it, and the parameters which will be sent to Parse.com will be like:
1 where={"post":{"$inQuery":{"where":{"image":{"$exists":true}}},"className":"p\ 2 osts"}}
• whereNotInQuery function: I think by the name we knows what it will do, its the opposite of thewhereInQueryfunction.
By now we have had fast overview of what doe theparseQueryclass has for us, and how to use it when querying data from Parse.com