Parse.com include related objects in one to many relationship

 ̄綄美尐妖づ 提交于 2019-12-11 19:26:23

问题


Supposedly I have a database structure like this

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

with its relationship like this

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

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.


回答1:


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.

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



来源:https://stackoverflow.com/questions/23556501/parse-com-include-related-objects-in-one-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!