本文介绍了Parse.com 在一对多关系中包含相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说我有一个这样的数据库结构

Supposedly I have a database structure like this

Person ('name', 'age')
Belonging ('person', 'type')

像这样的关系

Person -> Belongings (one to many)

我想查询所有人的物品数量,就像这样

and I want to query all person with their number of belongings included like this

Array[0] -> name, age, numberOfBelongings,
Array[1] -> name, age, numberOfBelongings,
etc

我该怎么做?我尝试使用CloudCode,但结果发现获取方法是异步的,所以我无法获取每个人的物品数量.

How do I do this? I tried to use CloudCode but, it turned out the fetching method is asynchronous, so I can't fetch the number of belongings to each person.

推荐答案

你不能直接这样做.如果您需要此计数,您可以在 Person 类(即 numberofbelongings)上创建一个字段,您可以在 Belonging 上的 afterSave 云代码挂钩中更新该字段.每当保存 Belonging 时,您还会增加 Person 对象上的 numberofbelongings 字段,并将其保存.

You can't do that directly. If you need this count, you could create a field on the Person class (i.e. numberofbelongings) which you update in an afterSave cloud code hook on Belonging. Whenever a Belonging is saved, you also increment the numberofbelongings field on the Person object, and save that as well.

没有额外的查询,没有来自您的设备的额外网络流量,并且您始终拥有可用于任何 Person 对象的计数.

No extra queries, no extra network traffic from your device, and your always have the count available for any Person object.

这篇关于Parse.com 在一对多关系中包含相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:45