代码之家  ›  专栏  ›  技术社区  ›  Eduardo

环回4:多对多关系

  •  0
  • Eduardo  · 技术社区  · 7 年前

    我正在尝试实现一种方法来过滤表中有许多关系的数据。

    我有以下表格工作,工作类别和类别。

    到目前为止,我在考虑使用job_id对job_类别进行查询,然后使用in()使用该结果添加一个条件,但我也没有找到任何方法来弹出此选项。

    问题:

    1. 如何在环回4中实现多人关系?

    2. 如何使用筛选查询?

    局部放电 我可以用$inq来回答第二个问题。

    filter.where = {
       ...filter.where,
       id: {inq: [2, 7]},
    };
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Samarpan    7 年前

    根据问题的上下文,可以在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控制器,并使用其中的find方法来获取数据。 不幸的是,lb4中尚未实现find方法的filter类中的include参数。它仍然在擦拭。参考 this 从loopback next repo获取更新的线程。在此之前,您可能必须添加自定义逻辑T控制器或存储库类才能实现此目的。 下面是我的两个建议方法。

    1. 配置属于存储库中的关系(请参阅文档 here )并在控制器内部使用它来获取相关数据的响应(请参阅实现 here )您可能需要为此创建自己的响应模型。为此,我们创建了自己的DTO。也可以返回“any”类型作为对此的响应,但不建议这样做。
    2. 如果需要,可以执行自己的连接查询。这是本机查询方法。但是,不幸的是,repository类中的execute函数还没有实现。见 here . 不过,它在DTS中可用。所以,我们实施了一项工作直到它被实施。我们创建了一个基本存储库类,它将由应用程序中的所有存储库类继承(替换所有 扩展DefaultCrudRepository 具有 扩展appdefaultcrudrepository )下面是基本存储库的实现。
        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);
          }
        }
    

    希望这对你的问题有帮助。对于问题2,您已经提到了该方法。那是有效的。

    推荐文章