我希望能够得到TaskCategory的相关标签列表。标签通过 Taggable 数据透视表与 TaskCategory 相关。
我有以下表格:
任务类别表
@DataClassName("TaskCategory")
class TaskCategories extends Table {
IntColumn get id => integer()();
TextColumn get title => text().nullable()();
@override
Set<Column> get primaryKey => {id};
}
任务表:TaskCategory 的子表。
@DataClassName("Task")
class Tasks extends Table {
IntColumn get id => integer()();
TextColumn get title => text().nullable()();
@JsonKey('task_category_id')
IntColumn get taskCategoryId => integer().nullable()();
@override
Set<Column> get primaryKey => {id};
}
标签表:标签可以附加到任何模型,不仅是任务表,这就是为什么这个表中没有定义相关的 id 列。但是与标签表的关系将在可标记(数据透视)表中定义。
@DataClassName("Tag")
class Tags extends Table {
IntColumn get id => integer()();
TextColumn get name => text().nullable()();
@override
Set<Column> get primaryKey => {id};
}
可标记表:用于标记和任何其他需要附加标记的模型的数据透视表。该表将负责从诸如 Tasks 或 TaskCategories 之类的模型或任何其他需要一些标签的模型中附加或删除标签。
@DataClassName("Taggable")
class Taggables extends Table {
@JsonKey('tag_id')
IntColumn get tagId => integer()();
@JsonKey('taggable_model_id')
IntColumn get taggableModelId => integer()(); // it can be id of Task or TaskCategory model or any other model id that need to have tag attached to it.
@JsonKey('taggable_model_type')
TextColumn get taggableModelType => text()(); // it store the type of model in string e.g 'task' for Task model and 'task_category' for TaskCategory model so that we can get tags for the model based on model's id and type
@override
Set<Column> get primaryKey => {tagId, tagableModelId, tagableModelType};
}
这就是我获得 taskCategory 的多个相关任务的方式,以便我可以在同一个位置显示 TaskCategories 及其任务。但我不知道如何也获取taskCategory的多个相关标签。
TaskCategoryWithTasksAndTags
class TaskCategoryWithTasksAndTags {
TaskCategoryWithTasksAndTags({
required this.taskCategory,
required this.tasks,
required this.tags,
});
final TaskCategory taskCategory;
final List<Task?> tasks;
final List<Tag?> tags;
}
Stream<List<TaskCategoryWithTasksAndTags>>
watchTaskCategoryWithTasksAndTags() {
final taskCategoriesStream = select(taskCategories).watch()
final tasksStream = select(tasks).watch();
final tags = // how to get taskCategories's related tags through taggables pivot table?
return Rx.combineLatest3(
taskCategoriesStream, tasksStream, tags, (
List<TaskCategory> taskCategories,
List<Task?> taskList,
List<Tag?> tagList,
) {
return taskCategories.map((taskCategory) {
final tasks = taskList
.where(
(task) =>
task?.taskCategoryId ==
taskCategory.id,
)
.take(6)
.toList();
final tags = tagList.where() // need help to get the tags attached/related to the taskCategory
return TaskCategoryWithTasksAndTags(
taskCategory: taskCategory,
tasks: tasks,
tags: tags,
);
}).toList();
});
}