2

I'm having a problem getting the values from a pointer column. There is a column named "User" that points to the "_User" class in parse. I'm attempting to get the username associated with each row in my locations database. But for some reason, I'm getting a strange response.

Call to a member function get() on a non-object

My code is:

$query = new ParseQuery("SignoutDestination");
   $query->includeKey("User");
   // Limit what could be a lot of points.
   $query->limit(100);
   // Final array of objects
   $signoutdestinations = $query->find();

   foreach($signoutdestinations as $obj){
     echo $obj->get("User")->get("username");
   }

Has there been a change in the SDK or anything that could be causing this? Or am I doing something wrong?

13
  • Maybe its because I don't speak PHP, but the thing that looks wrong to me is the assignment of the find() result. In the other SDK, find will run asynchronously, so anything you check related to $signoutdestinations won't be initialized. Commented Jul 30, 2015 at 23:06
  • No, that's not it. I can do echo $obj->get("User"); and it returns the array of objects fine, but for some reason I can't access the username. Commented Jul 30, 2015 at 23:08
  • Another fact from the other sdks, hopefully useful to you, is that username has its own accessor method (called username), not like other attributes where you would use get. Commented Jul 30, 2015 at 23:10
  • I wonder which get() it is... Is $obj actually an object? I'd var_dump($obj) or gettype($obj) to see. If it is, the call $obj->get('User') might not be returning an object. I'd try to debug by deduction there. Commented Jul 30, 2015 at 23:10
  • @JakeOls - you mentioned that $obj->get('User') returns an array of objects. If so, you'll have to iterate over that result set and call ->get('username') on each object. array is not an object and does not have a get() method, or any method for that matter. Commented Jul 30, 2015 at 23:12

1 Answer 1

3

I encountered the same issue and I kinda find a solution :

   $query = new ParseQuery("SignoutDestination");
   $query->includeKey("User"); // I didn't use it in my query
   // Limit what could be a lot of points.
   $query->limit(100);
   // Final array of objects
   $signoutdestinations = $query->find();

   foreach($signoutdestinations as $obj){
     $pointer = $obj->get("User");
     // It retrieves a pointer to object, so you
     // can only getObjectId() and specific fields
     // like this
     $pointer->fetch();
     $pointer->getUsername();
     // Now you're able to getUsername()
   }

I hope it will help you, I would have post it as comment but couldn't due to my freshly created account.

EDIT: I changed the code with something I tried on my Parse and it worked, but it just ruins your requests ratio because of fetching, find a clever way to get Username just by querying _User and comparing ObjectId.

Indeed, with the includeKey() it should work as you written it up...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I had to do this when getting the lat/long from a Parse Geopoint, but for some reason it still says "Call to a member function getUsername() on a non-object"... I opened an issue on github, but it was quickly closed :/ I'll update this thread in case I find a solution.

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.