问题描述
我正在尝试实现一种过滤具有多对多关系的表中数据的方法.
I am trying to implement a way to filter data in tables which have ManytoMany relationship.
我有以下表格job,job_category和category.
I have following tables job, job_category and category.
到目前为止,我正在考虑使用job_id对job_category进行查询,然后使用该结果通过IN()添加条件,但是我也找不到任何实现此选项的方法.
So far I am thinking in do a query to job_category using job_id and then use that result to add a condition using IN() but I do not find any way to impĺement this option either.
问题:
-
如何在Loopback 4中实现ManytoMany关系?
How to implement ManytoMany relation in Loopback 4?
如何使用IN过滤查询?
How to filter a query using IN?
PD 我可以使用$ inq作为问题编号2.
PDI can use $inq for question number 2.
filter.where = {
...filter.where,
id: {inq: [2, 7]},
};
推荐答案
您可以使用hasManyThrough关系在Loopback 4中实现多对多关系. hasManyThrough关系是hasMany关系的扩展.
You can implement a many-to-many relationship in Loopback 4 using the hasManyThrough relationship. The hasManyThrough relationship is an extension to the hasMany relationship.
当前,此功能是一个等待接受的请求请求.
Currently, this feature is a pull request waiting to be accepted.
https://github.com/strongloop/loopback-next/pull/2359
但是,此请求请求的代码已打包,可以通过以下方式安装和使用.
However, the code for this pull request has been packaged and can be installed and used the following way.
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
models/patient.model.ts
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[];
}
repositories/Patient.repository.ts
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
);
}
}
下面的链接中有一个基本示例.
There is a basic example of this at the following link.
https://github.com/codejamninja/medical-practice-api
请注意,此API可能会在接受拉取请求之前更改.
Please note that this api may change before the pull request is accepted.
您可以在以下链接中了解有关这种关系的更多信息.
You can read more about how this relationship works at the following links.
https://loopback.io/doc/en/lb3/HasManyThrough -relations.html https://guides.rubyonrails.org/association_basics.html#有很多通过关联
这篇关于环回4:多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!