0

In Parse's documentation, we see how to do an inverse of a many-to-many relationship but with just one object to look up. To work from their example of Books and authors, where they know the author and they want to find the books. What I want is to find the books that two or more authors have contributed to.

https://www.parse.com/docs/relations_guide#manytomany-relations

What I've tried is like the following code:

// suppose we have a author object, for which we want to get all books
PFObject *authorA = ...
PFObject *authorB = ...

// first we will create a query on the Book object
PFQuery *query = [PFQuery queryWithClassName:@"Book"];

// now we will query the authors relation to see if the author object
// we have is contained therein
[query whereKey:@"authors" equalTo:authorA];
[query whereKey:@"authors" equalTo:authorB];

The code works sort of.

But, what seems to be happening is if authorA has many books but authorB has one, one book is found in the query. If authorA has one book and authorB has many, many books are found in the query.

What I want is a method to find the books that have both authorA and authorB in its authors relation.

rici
  • 234,347
  • 28
  • 237
  • 341
AutomatonTec
  • 666
  • 6
  • 15

2 Answers2

0

you could use below piece of code :-

PFQuery *authorA_Query = [PFQuery queryWithClassName:@"Book"];
[query whereKey:@"authors" equalTo:authorA];

// PFQuery *authorB_Query = [PFQuery queryWithClassName:@"Book"];
[query whereKey:@"authors" equalTo:authorB];

// PFQuery *query = [PFQuery orQueryWithSubqueries:@[authorA_Query,authorB_Query]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
   // results contains book with both author A and B.
   // and this is called compound querying.
   // this way you could pass many queries to filter out your result.
 }];
nikhil84
  • 3,235
  • 4
  • 22
  • 43
  • But wouldn't that find books that written by authorA OR authorB? I want books that are written by authorA AND authorB. I'll try it in my code and see if this is the magic though :) – AutomatonTec Sep 29 '14 at 17:25
  • Tried it - I get all the books by authorA OR authorB. Not AND – AutomatonTec Sep 29 '14 at 17:28
  • I have made changes so check out ! so no need of orQuery method simply use that query object and provide your another condition and will work like AND which you want. Here is a [link](http://stackoverflow.com/questions/21964373/multiple-query-search-with-parse-com) for reference. – nikhil84 Sep 30 '14 at 04:27
  • Yep - that's the same as in my question. However, it still finds the union of the set of books of authorA and books of authorB, but weirdly. It seemed to give different results depending on order. What I was looking for was a method to books that work written by authorA and authorB... but at this stage, I've changed everything to use arrays. Perhaps I'll experiment with this again later. Thanks :) – AutomatonTec Sep 30 '14 at 05:46
0

It really looks like there isn't a solution using PFRelation (hope someone proves me wrong).

What I've done is fallen back to arrays.

https://www.parse.com/docs/relations_guide#onetomany-arrays

So something like this works

Author *authorA = ...
Author *authorB = ...
Book* book = ...

// stick the objects in an array
NSArray *authors = @[authorA, authorB];

// store the authors for the book
[book setObject:weapons forKey:@"authors"];

// then you can query as usual
PFQuery *bookQuery = [Book query];
[bookQuery whereKey:@"authors" equalTo:authorA];

// or query using an array of Author objects...
[bookQuery whereKey:@"authors" containedIn:@[authorA, authorB]];
[bookQuery whereKey:@"authors" containsAllObjectsInArray:@[authorA, authorB]];

However, it isn't the solution that I wanted. I was looking for the ability to have a large number of "authors" for a given "book". But for now, this will work...

AutomatonTec
  • 666
  • 6
  • 15