如何使用PHP从parse.com检索顶层对象中的关系中检索数据


How to I retrieve data from a Relation in a top-level object retrieved from parse.com using PHP

我正在尝试从使用parse.com和PHP的Relation检索数据

我可以毫无问题地获得顶层对象,但我无法访问相关数据。([myRelation] => stdClass Object ( [__type] => Relation [className] => other))

请参考下面我的代码:

$className = "myClass";
$url = 'https://api.parse.com/1/classes/' . $className;
$appId = 'xxxxxx'; 
$restKey = 'xxxxxxx';
$relatedParams = urlencode('include=people'); 
$rest = curl_init(); curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($rest, CURLOPT_URL, $url .'/io1GzkzErH/'.$relatedParams); 
curl_setopt($rest, CURLOPT_HTTPGET, true); 
curl_setopt($rest, CURLOPT_RETURNTRANSFER, true); 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; 

$echo的完整响应如下:

stdClass Object ( 
         [Altres] => 
                loremipsum.      
         [Categoria] => 
                Comedia 
         [Durada] => 
                120min 
         [Estat] => 
                Al teatre 
        [createdAt] => 
                2014-05-20T12:01:06.094Z 
        [objectId] => 
                 io1GzkzErH 
        [people] => 
                stdClass Object ( 
                              [__type] => 
                                     Relation [className] => 
                                                          persones )
         [updatedAt] => 
                   2014-05-20T12:07:22.758Z )

我如何从这个示例部分访问Relation [className] => persones中的属性?

[people] => 
    stdClass Object ( 
        [__type] => 
            Relation [className] => persones )

节选自parse.com文档

如果您想检索属于父对象的Relation字段成员的对象,您可以使用$relatedTo操作符。假设您有一个Post类和User类,其中每个Post都可以被许多用户喜欢。如果为某篇文章点赞的用户存储在该文章的like键下的关系中,您可以通过以下方式查找为某篇文章点赞的用户:

curl -X GET '
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" '
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" '
  -G '
  --data-urlencode 'where={"$relatedTo":{"object":
{"__type":"Pointer","className":"Post","objectId":"8TOXdXf3tz"},"key":"likes"}}' '
  https://api.parse.com/1/users

parse.com的API有点奇怪。你的对象被安排在many-to-one关系中,在这个例子中是people<persones>->class<Class>。您需要使用where子句查询表示one的类的api端点,并使用$relatedTo操作符。当你使用$relatedTo操作符时,你将指定一个objectId来唯一标识一个在一侧的实体,而指定一个key来标识的实体。它非常类似于SQL

中的join

下面的查询应该可以为您工作:

curl -X GET '
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" '
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" '
  -G '
  --data-urlencode 'where={"$relatedTo":{"object":
{"__type":"Pointer","className":"Class","objectId":"io1GzkzErH"},"key":"people"}}' '
  https://api.parse.com/1/classes/