gpt4 book ai didi

android - 如何从 firebase db android 中的多对多关系获取完整数据?

转载 作者:行者123 更新时间:2023-11-29 22:46:08 25 4
gpt4 key购买 nike

我有一个属于学校的类(class),这个类(class)有老师和学生,他们都有名字,可能还有电话号码,我想获得类(class)的完整数据但首先,你能给我一些建议吗,从以下 Dbs 中什么是最好的性能和维护:

第一个

 "schools":{
"school1":{
"class1":{
"name":"SC1",
"teachers":[{
"name":"T1"
}, {
"name":"T2"
}],
"students":[
{"name":"S1"},
{"name":"S2"}
]
}
}
.
.
.
.
.
.
.

}

和第二个

  "school":{
"school1":{
"name":"SC1"
},
"school2":{
"name":"SC2"
}
},
"classes": {
"class1": {
"name": "C1"
},
"class2": {
"name": "C2"
}
},
"students": {
"student1": {
"name": "S1",
"phone":"123456789"
},
"student2": {
"name": "S2",
"phone":"123456789"
},
"student3": {
"name": "S3",
"phone":"123456789"
},
"student4": {
"name": "S4",
"phone":"123456789"
}
},
"teachers": {
"student1": {
"name": "T1",
"phone":"123456789"
},
"student2": {
"name": "T2",
"phone":"123456789"
},
"student3": {
"name": "T3",
"phone":"123456789"
},
"student4": {
"name": "T4",
"phone":"123456789"
}
},
"classes_enrollments": {
"class1": {
"teacher1": true,
"teacher3": true,
"student1": true,
"student2": true
},
"class2": {
"teacher2": true,
"teacher4": true,
"student3": true,
"student4": true
},
"class3": {
"teacher1": true,
"teacher2": true,
"student3": true,
"student4": true,
"student1": true,
"student2": true
}
},
"student_friends": {
"student1": {
"student2": true
},
"students2": {
"student1": true,
"student3": true
},
"students3": {
"student2": true
}
},
"teacher_friends": {
"teacher1": {
"teacher2": true
},
"teacher2": {
"teacher1": true,
"teacher3": true
},
"teacher3": {
"teacher2": true
}
}

关于如何获取类(class)1的完整数据的第二种方式:在哪所学校及其教师和学生的姓名和人数以及他们的姓名和电话

谢谢

最佳答案

我会混合这两者。

为了代码简洁和单个类细节的阅读性能,第二种方案确实会很乱。第一种方案会更好,但有一些改进。

teachersstudents 路径保留在根目录下,就像在第二个方案中一样。

在根目录添加teacher_enrollmentsstudent_enrollments路径,保存每个老师/学生关联的类(class)id。

不要将类(class)教师和学生保存为类(class)内部的数组,而是使用映射,类似于您在根 teachersstudents 路径中保存的内容。

这样,当您从根路径编辑教师时,您还可以从注册路径中获取所有相关类(class)(id)的列表,并对这些类(class)进行多路径更新,以更新每个相关类(class)的教师/学生详细信息。

如果您有大量数据,您可能希望为类(class)摘要维护一个单独的路径,以便您可以轻松地显示类(class)列表,而无需下载所有包含的教师和学生的数据(这些数据将存在在所有这些类(class)中多次)。

当您删除一个类(class)时,您还需要执行多路径更新以删除所有关联的注册。 如果学生和教师的总人数不是太多,您可以删除所有教师/学生的注册。如果你有很多老师/学生,你可以保留你的 classes_enrollments 路径(但在 ids 之前有中间 teachersstudents),这样您可以仅使用所需的教师/学生 ID 进行更新。(实际上要简单得多。您在类(class)信息中已经有了教师/学生 ID)

// How to delete a class in JavaScript.
// For Java, use updateChildren() instead of update(),
// and supply it with a HashMap instead of a plain object.
const classToDelete = { id: 'class1', teachers: ..., students: ..., school: ... };
const updateObject = {
['classes/'+classToDelete.id]: null },
['schools/'+classToDelete.school.id+'/classes/'+classToDelete.id]: null },
};
Object.keys(classToDelete.teachers).forEach(teacherId => {
updateObject['teachers/'+teacherId +'/classes/'+classToDelete.id] = null;
});
Object.keys(classToDelete.students).forEach(studentId=> {
updateObject['students/'+studentId+'/classes/'+classToDelete.id] = null;
});
dbRef.update(updateObject);

示例数据库结构(与说明略有不同,但使用相同的概念):

"schools": {
"school1": {
"id": "school1",
"name": "The best school",
"classes": {
"class1": {
"id": "class1",
"name": "The best class"
}
}
}
},
"classes": {
"class1": {
"id": "class1",
"name": "The best class",
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123"
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456"
}
},
"school": {
"id": "school1",
"name": "The best school"
}
}
},
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
}

祝你好运!

关于android - 如何从 firebase db android 中的多对多关系获取完整数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com