问题描述
我有一个属于学校的班级,这个班级有老师和学生,他们每个人都有姓名,也许有电话号码,我想获取班级的完整数据但是首先,您能否建议我,从以下Db中获得最好的性能和维护:
I have a class which is part of a school and this class has teachers and students, all of them has name and maybe has phone number , I want to get the full data for the classesbut firstly, could you advice me, what is the best for performance and maintaining from the following 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
}
}
第二种方法如何获取班级1的完整数据:在哪个学校,学校的名称和数量以及老师和学生的姓名和电话
and for the 2nd way how to get the full data for the class1: in which school and it's name and count of teachers and students and their names and phones
谢谢
推荐答案
我将两者混合使用.
为了简化代码并读取单个类详细信息的性能,第二种方案确实很杂乱.第一种方案会更好,但是会有一些改进.
For code simplicity and reading performance of individual class details, the 2nd scheme would indeed be messy. The 1st scheme would be better, but with some improvements.
像在第二个方案中一样,保持teachers
和students
路径在根目录.
Keep the teachers
and students
paths at root, just like in the 2nd scheme.
在根目录添加teacher_enrollments
和student_enrollments
路径,以保存每个教师/学生所关联的班级的ID.
Add teacher_enrollments
and student_enrollments
path at root, to save the ids of the classes that each teacher/student is associated with.
不要将班级老师和学生保存为班级内的数组,而应使用地图,类似于您在根teachers
和students
路径中保存的内容.
Don't save class teachers and students as arrays inside classes, but use maps instead, similar to what you're saving in the root teachers
and students
path.
这样,当您从根路径编辑老师时,您还可以从注册路径中获取所有与其相关的班级列表(ID),并对这些班级进行多路径更新,以更新每个相关课程中的老师/学生详细信息.
That way, when you edit a teacher from the root path, you can also get a list of all their associated classes (the ids) from the enrollments path, and do a multi-path update for these classes, to update the teacher/student details in each associated class.
如果您有大量数据,则可能需要为班级摘要维护一个单独的路径,以便可以轻松显示班级列表,而不必下载所有所包括的老师和学生的数据(将会存在在所有这些类中多次.
If you have lots of data, you might want to maintain a separate path for class summaries, so that you can easily show a list of classes, without having to download the data for all included teachers and students (which would be present multiple times in all these classes).
删除班级时,您还希望进行多路径更新以删除所有关联的注册. (实际上要简单得多.班级信息中已经有老师/学生的ID)
When you delete a class, you would also want to do a multi-path update to delete all associated enrollments. (it's actually a lot simpler. You already have the teacher/student IDs in the class info)
// 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);
示例数据库结构(与指示的数据库结构略有不同,但使用相同的概念):
Example database structure (slightly different than instructed, but using the same concepts):
"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"
}
}
}
}
}
祝你好运!
这篇关于如何从Firebase DB Android中的多对多关系中获取完整数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!