3

I have a Photo Class and a Comments Class. I'm trying to implement a search where a user can retrieve Photos that contain Comments with certain words in them. I've thought of several ways to go about this and the simplest seems to be to include an array of Comments objects in the Photo Class.

However, I'm having trouble figure out how to query this array of comment objects to query their key 'content'.


Doing something like creating an array of the comment strings themselves would work, but I'd rather save a pointer to the comments for reasons of consistency.

2 Answers 2

8

Timothy is correct, so feel free to vote for whatever (never bad to share the love), I'm just paraphrasing.

You build a query against your comments table and then match it up to your photos.

PFQuery *commentsQuery = [PFQuery queryWithClassName:@"Comments"];

//you can keep entering more or queries to get more terms
[commentsQuery whereKey:@"content containsString:"searchterm"]

PFQuery *photosQuery = [PFQuery queryWithClassName:@"Photos"];
[photosQuery whereKey:@"_id" matchesKey:@"photo" inQuery:commentsQuery]
[photosQuery fetch];
Sign up to request clarification or add additional context in comments.

3 Comments

The only reason I didn't provide sample code is that I was trying to force the OP (and anyone else that is interested in this question) to read the basic documentation, as if they had read it they would not need to ask here. I like your answer, and would usually provide sample code too.
Yeah I don't normally double up on answers, and I agree, not all answers require code. I did it mostly to just practice myself!
Yeah, I find answering questions on here one of the best way to improve my skills too!
2

The help documentation on Relational Queries is what you will want to have a look through.

Basically you create a query for your comments array/relation, and then use the photosQuery.matchesQuery("comments", commentsQuery) call to limit photos to those where the comments match your sub-query.

Comments

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.