我正在尝试实现一种在具有ManytoMany关系的表中筛选数据的方法。
我有以下表--作业、job_category和类别。
到目前为止,我正在考虑使用job_id对job_category执行一个查询,然后使用这个结果添加一个使用IN()的条件,但是我也没有找到任何方法来实现这个选项的impĺ。
问题:
PD I可以用$inq来回答问题2。
filter.where = {
...filter.where,
id: {inq: [2, 7]},
};发布于 2019-04-01 10:43:49
考虑到您问题的上下文,可以在lb4中实现多到多的关系,如下所示。
就业模式(样本)-
@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);
}
}类别模型(样本)-
@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关系。
@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控制器或存储库类来实现这一点。下面是我建议的两种方法。
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,你已经提到了这个方法。这是可行的。
发布于 2019-09-04 06:13:51
您可以使用hasManyThrough关系在Loopback 4中实现多到多的关系。hasManyThrough关系是对hasMany关系的扩展。
目前,此功能是等待接受的拉请求。
https://github.com/strongloop/loopback-next/pull/2359
但是,此拉请求的代码已经打包,可以按以下方式安装和使用。
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模型/病人。模型。
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[];
}资料库/病人.储存库
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#拥有多个通过关联的
https://stackoverflow.com/questions/55114484
复制相似问题