简体   繁体   English

parse.com嵌套对象_encode()并解码为php数组无法正常工作

[英]parse.com nested objects _encode() and decode to php array not working properly

I have a ParseObject called Follow like explained in this JS tutorial https://parse.com/docs/js/guide#relations-using-join-tables 我有一个名为Follow的ParseObject,如该JS教程https://parse.com/docs/js/guide#relations-using-join-tables中所述

Although I'm doing it in PHP. 虽然我是用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. 目标是保存跟随对象后,便可以使用嵌套类获取该对象的php数组表示形式。 The from and to properties of the Follow object are pointers to the _User object. Follow对象的fromto属性是_User对象的指针。

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. 此代码块创建了一个跟随对象,然后尝试使用json_decode将对象转换为php数组,但是只有顶层对象才被正确解码。

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-> output是一个php数组,后来通过框架转回json。

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. 如您所见,from和to字段只是字符串文字,并且不会解码为php数组。

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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM