hasMany关系:包括从另一个方向 [英] hasMany relation: including from the other direction

查看:79
本文介绍了hasMany关系:包括从另一个方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有下一个模型:

user.json:
{//...
    "relations":{
        "invoices": {
            "type": "hasMany",
            "model": "Invoice",
            "foreignKey": "receiverId"
        },
    }
//...
}

又名用户可能有很多发票.这段代码将字段ReceiverId添加到发票模型中.

A.k.a. a user might have many invoices. This code adds the field receiverId to the invoice model.

现在,我想获取一张发票清单,包括其收货人.我该怎么办?

Now I want to get a list of invoices including their receivers. How can I do that?

Invoice.find({include: "reciever"})

Invoice.find({include: "user"})

不起作用,返回:未为发票模型定义关系\"接收器\"错误.

Did not work, returned: "Relation \"receiver\" is not defined for Invoice model" error.

感谢您的帮助.

推荐答案

您必须定义关系.

invoice.json:

invoice.json:

{//...
    "relations":{
        "receiver": {
            "type": "belongsTo",
            "model": "Receiver"
        },
    }
//...
}

然后您可以像这样查询模型:

Then you can query your model like this:

Invoice.find({include: "receiver"}, function(data){ 
   console.log(data);
});

这篇关于hasMany关系:包括从另一个方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆