Adonis-Js:获取与另一个实体相关的特定数量的实体

问题描述

我需要您在Adonis-Js中的帮助来加载具有关系的实体。这是我的问题:

我有两个实体:

  • 联系人(属于联系人组)
  • 联系人组(有很多联系人)

我想请求获取其中包含一些 Contact Contactgroup 列表,这是一个示例,此代码为我提供了所有 Contactgroup ,并与他们相关的所有联系人。 :

    let ContactgroupList = await Contactgroup.query()
        .where('profile_id',auth.user.id)
        .with('contact')
        .fetch();

就我而言,我只想让所有 Contactgroup 都只包含与之相关的一些 Contact (例如,最多3个 Contact 找到每个 Contactgroup ),但是当我使用以下代码时:

    let ContactgroupList = await Contactgroup.query()
        .where('profile_id',auth.user.id)
        .with('contact',(builder) => {
          builder.pick(3)
        })
        .fetch();

我仅获得与第一个联系人组相关的前三个联系人,例如:

如果联系人组[0] 具有X个联系人->,则会显示前三个联系人的数组,并且 如果 Contactgroup [1] 具有X个 Contact ->,它将显示一个空数组。

如何为每个联系人组获得3个联系人

我希望我的解释清楚,并在此先感谢您的帮助!

解决方法

我的一个朋友使用 eagerLoadQuery 给了我解决方案:

public class Shape {

    double width,height;

    public Shape(double w,double h)
    {
        this.height = h;
        this.width = w;
    }
    public void area(Object shape){ // area method of parent class
       shape.area(); // here I am getting error.
    }
}
class triangle extends Shape{

    triangle tri;
    public triangle(double w,double h) {
        super(w,h);
    }
    public void area()// area method of derived class
    {
        double area = (1/2)*width*height;
        System.out.println("The area of triangle is: "+area);
    }
}

class rectangle extends Shape{

    rectangle rect;
    public rectangle(double w,h);
    }
    public void area() // area method of derived class
    {
        double area = width*height;
        System.out.println("The area of rectangle is: "+area);
    }
}

另请参阅此帖子以获取更多信息:Link