0

我正在我的环回 4 应用程序中设置多对多关系。目前我正在使用这个答案作为指南,但是在创建存储库和控制器之后,我不知道如何继续。

目前我有三个关系表:CourseLanguageLanguageCourse。这意味着一个Course可以有多种语言,一个Language可以属于许多课程。

我的language-course.model.ts样子是这样的:

import {Course} from './course.model';
import {Language} from './language.model';

@model({settings: {}})
export class LanguageCourse extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  @belongsTo(() => Course)
  courseId?: number;

  @belongsTo(() => Language)
  languageId?: number;

  constructor(data?: Partial<LanguageCourse>) {
    super(data);
  }
}

export interface LanguageCourseRelations {
  // describe navigational properties here
}

export type LanguageCourseWithRelations = LanguageCourse &
  LanguageCourseRelations;

我的 course.model.ts 看起来像这样(我已经在这个模型中建立了一对多的关系):

import {User, UserWithRelations} from './user.model';

@model({settings: {}})
export class Course extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  @property({
    type: 'string',
  })
  name?: string;

  @property({
    type: 'string',
  })
  description?: string;

  @belongsTo(() => User)
  userId?: number;

  @property({
    type: 'string',
    default: 'active',
  })
  state?: string;

  @property({
    type: 'string',
    default: 'rookie',
  })
  level?: string;

  @property({
    type: 'string',
  })
  course_photo?: string;

  constructor(data?: Partial<Course>) {
    super(data);
  }
}

export interface CourseRelations {
  user?: UserWithRelations;
}

export type CourseWithRelations = Course & CourseRelations;

我的 language.model.ts 看起来像这样:


@model({settings: {}})
export class Language extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  @property({
    type: 'string',
  })
  name?: string;

  constructor(data?: Partial<Language>) {
    super(data);
  }
}

export interface LanguageRelations {
  // describe navigational properties here
}

export type LanguageWithRelations = Language & LanguageRelations;

我想向GET例如/courses/{id}端点(/courses以及)发出请求,并在响应中包含课程所具有的所有语言,但我不知道如何使其工作。我也希望它在/languages端点中工作。

谢谢你的时间!

4

1 回答 1

0

不幸的是,loopback 4 文档中没有定义适当的默认多对多关系。

但似乎您已经尝试实施以创建一个language-course.model.ts很好的桥接模型。现在您必须为此模型创建存储库和控制器。完成后,您可以编写自己的自定义逻辑来处理数据的存储和检索。

例如,正如您所说,您希望根据课程 ID 查询和获取所有语言的所有列表,您的 CourseLanguage 模型应该类似于

import {Course} from './course.model';
import {Language} from './language.model';

@model({settings: {}})
export class LanguageCourse extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  @property({
    type: 'string',
  })
  courses: string;

  @property({
    type: 'array',
    itemType: 'string',
    required:true
  })
  languages: string[];

  constructor(data?: Partial<LanguageCourse>) {
    super(data);
  }
}

export interface LanguageCourseRelations {
  // describe navigational properties here
}

export type LanguageCourseWithRelations = LanguageCourse &
  LanguageCourseRelations;

我们在上面所做的是我们正在创建两个新属性courses,它们将是课程的 ID,languages它将是一个包含该课程所有语言的数组。一旦完成。

接下来,您将不得不为此模型处理控制器按照以下步骤获取基于课程 ID API 的所有语言

/course/{id}使用您提到的路线创建一个自定义控制器。

在该控制器中,编写您的自定义代码,如下所示:

@get('/course/{id}', {
    responses: {
      '204': {
        description: 'Fetching the list of all the languages as per course ID',
      },
    },
  })
  async fetchListOfLanguagesAsPerCourseId(@param.path.string('id') id: string) {
  /** 
  * Search for the course in `CourseLanguage` collection or table by using the course id 
  **/
  let searchedCourseLanguageObject = await this.courseLanguageRepository.findOne({where: {courses: id}});

  /** 
  * If the course does not exists, throw an error 
  **/
  if(searchedCourseLanguageObject === null || undefined) {
    throw new HttpErrors.NotFound("Course does not have any languages")
  }

  /** 
  * If the course exists, it will have array languages with all the IDs of the languages from LAnguages collection or table in the database related to the course
  **/
let arrayOfLanguagesId = searchedCourseLanguageObject.languages;

/** 
  * Now go to the language table and fetch all the languages object whose IDs are present in the above array
  **/
  let listOfLanguages = await this.languagesRepository.find({where: {id: { inq: arrayOfLanguagesId }}});

/** 
  * Above code will look for all the languages ID in the languages table and return back objects of the language whose ID exist in the array arrayOfLanguagesId
  **/
return listOfLanguages

  }

希望这不会太吓人,当您实施它时,它很容易。如果您仍然面临任何问题,请写信给我。谢谢希望这会有所帮助。

于 2019-10-18T10:35:48.323 回答