如何查询解析php sdk获取ParseRelation的所有关键字?

时间:2017-11-06 09:07:57

标签: php parse-platform

我有2张桌子tblAlbumtblProduction,相册与tblProduction有关系。 (album_title,album_id,制作(ParseRelation的种类),...)

我想写一个查询来获取包含所有制作键的专辑?
像这样(album_title,album_id,production_name,production_title,...)

注意 - 我正在使用Parse PHP SDK。

1 个答案:

答案 0 :(得分:0)

我不确定您是否production设置为指针关系,但我会在此详细说明。< / p>

作为指针(1-1),您只需像这样调整查询。

$query = new ParseQuery('Album');

// include the nested object under 'production'
$query->includeKey('production');

// get albums with Production set under 'production'
$albums = $query->find();

作为关系(1-many),您需要进行单独的查询以获取所有关联的production个对象。

$relation = $albums->getRelation('production'); // can add Production as the class name as 2nd arg if you have issues
$relationQuery = $relation->getQuery();

// get all Production objects
$productions = $relationQuery->find();

如果您不断为每个Album进行此关系查询,则可以将开销降低到大约2个查询。只需在Production中添加指针即可返回其父Album,并使用ParseQuery::orQueries所有您的关系查询加入一个方便的查询中。

// get albums
$query = new ParseQuery('Album');
$albums = $query->find();

// get each relation query for each album
$queries = [];
foreach($albums as $album) {
    $relation = $album->getRelation('production', 'Production');
    $queries[] = $relation->getQuery();
}

// combine your relation queries
$query = ParseQuery::orQueries($queries);

// include the parent 'album' pointer so we can
// sort which objects belong where
$query->includeKeys('album');

// get these productions
$productions = $query->find();

您也可以使用matchesKeyInQuery执行某些操作,但我建议您阅读docs以获取有关详细信息。