1
votes

I am developing a backend for web app with Parse.com. I have Sport and Stream classes. Sport contains many Streams. Sport table has column streams, which is an array of pointers:

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

I am creating a new Stream inside a sport. Then I want to update my parent Sport object, and try to add pointer with php:

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

New stream is created, and i get the id back and printed. But the parent Sport is not updated. I get Error 500 Must use setArray() or setAssociativeArray() for this value.

I've tried AddUnique but still no luck. Please help!

UPDATE:

I've tried REST API, with this code:

        $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;

Server returns invalid jason error, code 107. I can not get the quotes work. What is the proper syntax for the $updatedData?

Response of the server: {"code":107,"error":"invalid json: {\"streams\": '{ \"__op\" : \"AddRelation\",\n \"objects\" : [ { \"__type\" : \"Pointer\", \"className\" : \"Stream\", \"objectId\" : \"CVymNF0HGh\"}] }'"}

2
You should be using json_encode() to make the JSON for you, do not manually create JSON. Create an array, set it how you like, then call json_encode(). - Rocket Hazmat

2 Answers

0
votes

Try this (I added one "}" at the end):

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

In the end this code worked:

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