如何在parse.com上添加指针数组指针?

时间:2014-09-04 10:52:03

标签: php pointers yii parse-platform

我正在使用Parse.com开发Web应用程序的后端。 我有Sport和Stream课程。体育包含许多流。运动表有列,这是一个指针数组:

[{"__type":"Pointer","className":"Stream","objectId":"VTq7sRxzzi"},
{"__type":"Pointer","className":"Stream","objectId":"8xJlFbq3YA"}]

我正在为一项运动创造一个新的Stream。然后我想更新我的父Sport对象,并尝试用php添加指针:

$parentSport->streams = array("__op"=>"AddRelation","objects"=>array(array("type" => "Pointer","className"=>"Stream","objectId"=>$newStream->getObjectId())));

创建新流,我将ID返回并打印出来。但是父母体育没有更新。 我得到错误500 必须对此值使用setArray()或setAssociativeArray()。

我尝试过AddUnique但仍然没有运气。请帮忙!

更新

我尝试过REST API,使用以下代码:

        $className = "Sport";

        $objectIdToEdit = $id;

        $url = 'https://api.parse.com/1/classes/' . $className . '/' . $objectIdToEdit;

        $appId = '';
        $restKey = '';

        //this is succeeded - the name got updated
        //$updatedData = '{"name":"someNewValue"}';

        //but this gives error "code":107,"error":"invalid json 
        $updatedData = '{"streams": { "__op" : "AddRelation",
             "objects" : [ { "__type" : "Pointer", "className" : "Stream", "objectId" : "".$newStreamId.""}] }'; 


        $rest = curl_init();
        curl_setopt($rest,CURLOPT_URL,$url);
        curl_setopt($rest,CURLOPT_PORT,443);
        curl_setopt($rest,CURLOPT_CUSTOMREQUEST,"PUT");
        curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($rest,CURLOPT_POSTFIELDS,$updatedData);
        curl_setopt($rest,CURLOPT_HTTPHEADER, 
            array("X-Parse-Application-Id: " . $appId,
                "X-Parse-REST-API-Key: " . $restKey,
                "Content-Type: application/json"));

        $response = curl_exec($rest);
        echo $response;

服务器返回无效的jason错误,代码107.我无法获得引号工作。 $ updatedData的正确语法是什么?

服务器的响应: {“code”:107,“error”:“json:{\”streams \“:'{\”__ op \“:\”AddRelation \“,\ n \”objects \“:[{\”__ type \ “:\”指针\“,\”className \“:\”Stream \“,\”objectId \“:\”CVymNF0HGh \“}]}'”}}

2 个答案:

答案 0 :(得分:0)

试试这个(我在最后添加了一个"}")

'{"streams": { "__op" : "AddRelation",
         "objects" : [ 
             { "__type" : "Pointer", 
             "className" : "Stream",
             "objectId" : "".$newStreamId.""
             }
            ] 
        }}'; 

答案 1 :(得分:0)

最后这段代码有效:

$updatedData = '{"streams": { "__op" : "AddUnique",
     "objects" : 
     [ 
         { "__type" : "Pointer", 
         "className" : "Stream",
         "objectId" : "'.$newStreamId.'"
         }
    ] 
    }}';