首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回环4:多对多关系

回环4:多对多关系
EN

Stack Overflow用户
提问于 2019-03-12 05:02:22
回答 2查看 3.5K关注 0票数 3

我正在尝试实现一种在具有ManytoMany关系的表中筛选数据的方法。

我有以下表--作业、job_category和类别。

到目前为止,我正在考虑使用job_id对job_category执行一个查询,然后使用这个结果添加一个使用IN()的条件,但是我也没有找到任何方法来实现这个选项的impĺ。

问题:

  1. 如何在回溯4中实现ManytoMany关系?
  2. 如何使用IN筛选查询?

PD I可以用$inq来回答问题2。

代码语言:javascript
复制
filter.where = {
   ...filter.where,
   id: {inq: [2, 7]},
};
EN

回答 2

Stack Overflow用户

发布于 2019-04-01 10:43:49

考虑到您问题的上下文,可以在lb4中实现多到多的关系,如下所示。

就业模式(样本)-

代码语言:javascript
复制
    @model({
      name: 'jobs',
    })
    export class Job extends Entity {
      @property({
        type: 'number',
        id: true,
      })
      id: number;

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

      // Other columns of the table.....

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

类别模型(样本)-

代码语言:javascript
复制
    @model({
      name: 'categories',
    })
    export class Category extends Entity {
      @property({
        type: 'number',
        id: true,
      })
      id: number;

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

      // Other columns of the table.....

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

在作业类别关系模型中,我们将用作业模型和类别模型实现属于关系。这将确保m:n关系。

代码语言:javascript
复制
    @model({
      name: 'job_categories',
    })
    export class JobCategory extends Entity {
      @property({
        type: 'number',
        id: true,
      })
      id: number;

      @belongsTo(() => Job)
      job_id: number;

      @belongsTo(() => Category)
      category_id: number;

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

现在,使用lb4 CLI,您可以为作业类别模型创建一个存储库和REST控制器,并使用那里的查找方法来获取数据。不幸的是,Filter类中包含参数for find方法还没有在lb4中实现。仍然是WIP。请参考线程从环回-下一个回购更新。在此之前,您可能需要添加自定义逻辑t控制器或存储库类来实现这一点。下面是我建议的两种方法。

  1. 配置属于存储库中的关系(参考文档这里),并在控制器内部使用它来获取相关数据(参考实现这里)。您可能需要为此创建自己的响应模型。我们为此目的创建了自己的DTO。您也可以返回“任意”类型作为响应,但这是不建议的。
  2. 如果需要,可以执行自己的联接查询。这是本机查询方法。但是,不幸的是,存储库类中的执行函数还没有实现。见这里。不过,它可以在dts中使用。所以,我们执行了一项工作,直到它实现。我们创建了一个基本存储库类,它将由应用程序中的所有存储库类继承(将所有扩展DefaultCrudRepository替换为扩展AppDefaultCrudRepository)。下面是基本存储库的实现。
代码语言:javascript
复制
    export abstract class AppDefaultCrudRepository<
      T extends Entity,
      ID
    > extends DefaultCrudRepository<T, ID> {
      constructor(
        entityClass: typeof Entity & {
          prototype: T;
        },
        dataSource: AppDataSource,
      ) {
        super(entityClass, dataSource);
      }

      execute(
        command: Command,
        parameters: NamedParameters | PositionalParameters,
        options?: Options,
      ): Promise<AnyObject> {
        // Commented below statement until it is implemented in lb4
        // return super.execute(command, parameters, options);
        return this.dataSource.execute(command, parameters, options);
      }
    }

希望这对你的问题1有所帮助。对于问题2,你已经提到了这个方法。这是可行的。

票数 5
EN

Stack Overflow用户

发布于 2019-09-04 06:13:51

您可以使用hasManyThrough关系在Loopback 4中实现多到多的关系。hasManyThrough关系是对hasMany关系的扩展。

目前,此功能是等待接受的拉请求。

https://github.com/strongloop/loopback-next/pull/2359

但是,此拉请求的代码已经打包,可以按以下方式安装和使用。

代码语言:javascript
复制
npm install --save @loopback/repository@git+https://git@github.com/codejamninja/loopback-next.git#npm/codejamninja/has-many-through-using-has-many@1.11.0-rc.1

模型/病人。模型。

代码语言:javascript
复制
import { Entity, model, property, hasMany } from '@loopback/repository';
import { Appointment, Patient } from '../models';

@model()
export class Physician extends Entity {
  @property({
    type: 'string',
    id: true
  })
  id?: string;

  @hasMany(() => Patient, { through: () => Appointment })
  patients: Patient[];
}

资料库/病人.储存库

代码语言:javascript
复制
import {
  DefaultCrudRepository,
  HasManyThroughRepositoryFactory,
  repository
} from '@loopback/repository';
import { inject, Getter } from '@loopback/core';
import { MemoryDataSource } from '../datasources';
import { Patient, Physician } from '../models';
import { AppointmentRepository, PhysicianRepository } from '../repositories';

export class PatientRepository extends DefaultCrudRepository<
  Patient,
  typeof Patient.prototype.id
> {
  public readonly physicians: HasManyThroughRepositoryFactory<
    Physician,
    typeof Patient.prototype.id
  >;

  constructor(
    @inject('datasources.memory')
    dataSource: MemoryDataSource,
    @repository.getter('AppointmentRepository')
    getAppointmentRepository: Getter<AppointmentRepository>,
    @repository.getter('PhysicianRepository')
    getPhysicianRepository: Getter<PhysicianRepository>
  ) {
    super(Patient, dataSource);
    this.physicians = this.createHasManyThroughRepositoryFactoryFor(
      'physicians',
      getPhysicianRepository,
      getAppointmentRepository // notice the through repository getter
    );
  }
}

在下面的链接中有一个基本的例子。

https://github.com/codejamninja/medical-practice-api

请注意,在接受拉请求之前,此api可能会更改。

您可以在以下链接中了解有关这种关系如何工作的更多信息。

https://loopback.io/doc/en/lb3/HasManyThrough-relations.html Basics.html#拥有多个通过关联的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55114484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档