我正在尝试使用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 )
答案 0 :(得分:0)
如果要检索作为父对象的Relation字段成员的对象,可以使用$ relatedTo运算符。想象一下,你有一个Post类和User类,许多用户都可以喜欢每个Post。如果喜欢帖子的用户存储在关键字下的帖子的关系中,您可以找到喜欢特定帖子的用户:
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
唯一标识关系 one 一侧的实体和key
,表示很多方面。它与SQL
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/