0

I have a ParseObject called Follow like explained in this JS tutorial https://parse.com/docs/js/guide#relations-using-join-tables

Although I'm doing it in PHP. The goal is once a Follow object is saved be able to get a php array representation of the object with the nested classes. The from and to properties of the Follow object are pointers to the _User object.

This block of code create a follow object, then attempts to convert to object to a php array using json_decode, but only the top level object is correctly decoded.

public function post_follow() {
    try {

        $user = $this->get_user(Input::post('objectId'));

        $follow = new ParseObject("Follow");
        $follow->set("from", $this->currentUser);
        $follow->set("to", $user);
        $follow->save();

        $this->output = json_decode($follow->_encode(),true);
    }
    catch(ParseException $ex) {

        return $this->error($ex);
    }
}

where $this->output is an php array that is later turned back into json via the framework.

This method has the output

    {
  "objectId": "6Gb1WflPfw",
  "createdAt": {
    "date": "2015-08-16 22:29:48.445000",
    "timezone_type": 2,
    "timezone": "Z"
  },
  "updatedAt": {
    "date": "2015-08-16 22:29:48.445000",
    "timezone_type": 2,
    "timezone": "Z"
  },
  "from": "{\"objectId\":\"88D437QDxp\",\"createdAt\":{\"date\":\"2015-08-13 08:26:29.478000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"updatedAt\":{\"date\":\"2015-08-16 20:09:17.048000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"email\":\"brian@mail.com\",\"emailVerified\":true,\"followersCount\":1,\"followingCount\":2,\"friendsCount\":18,\"phone\":null,\"username\":\"brian\"}",
  "to": "{\"objectId\":\"cmX8o9sEDh\",\"createdAt\":{\"date\":\"2015-08-13 08:29:54.735000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"updatedAt\":{\"date\":\"2015-08-13 08:30:17.188000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"email\":\"brian+2@mail.com\",\"emailVerified\":true,\"phone\":null,\"username\":\"brian2\"}"
}

As you can see the from and to fields are just string literals and not decoded into a php array.

Brian
  • 4,328
  • 13
  • 58
  • 103

1 Answers1

0

Try this if it works.

public function post_follow() {
   try {

    $user = $this->get_user(Input::post('objectId'));

    $follow = new ParseObject("Follow");
    $follow->set("from", $this->currentUser);
    $follow->set("to", $user);
    $follow->save();

    $this->output = json_decode($follow->_encode()); // add true in the second parameter.  $this->output = json_decode($follow->_encode(), true);
}
catch(ParseException $ex) {

    return $this->error($ex);
}

}

aldrin27
  • 3,407
  • 3
  • 29
  • 43