2

I have the following tables name "aniStudii" and "discipline", I have made a screenshot as well:

enter image description here

As you can see, there is a relation between these tables, at the "materii" column. The row from "aniStudii" with the column "Anul I" has a value and the other column has a different value, values that can be found in the "discipline" table.

I am using this query to get the values, but all I get is Error: bad pointer for key: _p_materii (Code: 106, Version: 1.2.8)

This is my query:

PFQuery *query = [PFQuery queryWithClassName:@"aniStudii"]; //1
PFObject *aniStudiu = [PFObject objectWithClassName:@"discipline"];
[query whereKey:@"materii" equalTo:aniStudiu];

[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
    NSLog(@"%@",results);
}];

Where is the problem? A big thanks in advance.

1 Answer 1

2

Do something like this, where you start from a specified object (which you may need a query to find):

PFObject *sourceObject = ...;

PFRelation *relation = [sourceObject relationforKey:@"materii"];

[[relation query] findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
    NSLog(@"%@",results);
}];

To get the first object you can perform a query something like:

PFQuery *query = [PFQuery queryWithClassName:@"aniStudii"];
[query whereKey:@"numeAn" equalTo:@"######"];
Sign up to request clarification or add additional context in comments.

5 Comments

PFObject *sourceObject = [PFObject objectWithClassName:@"aniStudii"]; PFRelation *relation = [sourceObject relationforKey:@"materii"]; [[relation query] findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) { NSLog(@"%@",results); }]; And I get 2013-05-09 11:53:40.438 licentaUser[2873:1b03] Error: a valid pointer is needed for RelatedTo operator (Code: 102, Version: 1.2.8) 2013-05-09 11:53:40.440 licentaUser[2873:c07] (null)
You need an instance of PFObject which has been retrieved from the server. You can't just create a new instance (which is what objectWithClassName does) and then query the relation.
PFQuery *query = [PFQuery queryWithClassName:@"aniStudii"]; PFObject *sourceObject = [query getObjectWithId:@"xWMyZ4YEGZ"]; PFRelation *relation = [sourceObject relationforKey:@"materii"]; [[relation query] findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) { NSLog(@"%@",results); }]; This works, but I have to know the objectId to make the result happen. I don't want that, is there anyway to go around this?
You can query for the source object by name or creation date (or any other field it has). Or, you can query for all instances of a particular object (paginated) and display them for the user to choose...
query hasn't got any other options besides getObjectWithId. So instead of ` PFObject *sourceObject = [query getObjectWithId:@"xWMyZ4YEGZ"];` I don't know what else to use.

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.