hasMany 关系:包括从另一个方向
hasMany relation: including from the other direction
假设我有下一个模型:
user.json:
{//...
"relations":{
"invoices": {
"type": "hasMany",
"model": "Invoice",
"foreignKey": "receiverId"
},
}
//...
}
A.k.a。一个用户可能有很多发票。此代码将字段 receiverId 添加到发票模型。
现在我想获取包括收款人在内的发票清单。我该怎么做?
Invoice.find({include: "reciever"})
或者
Invoice.find({include: "user"})
没用,返回:"Relation \"receiver\" is not defined for Invoice model" 错误。
感谢您的帮助。
您必须在发票模型中定义 belongsTo 关系。
invoice.json:
{//...
"relations":{
"receiver": {
"type": "belongsTo",
"model": "Receiver"
},
}
//...
}
然后你可以这样查询你的模型:
Invoice.find({include: "receiver"}, function(data){
console.log(data);
});
假设我有下一个模型:
user.json:
{//...
"relations":{
"invoices": {
"type": "hasMany",
"model": "Invoice",
"foreignKey": "receiverId"
},
}
//...
}
A.k.a。一个用户可能有很多发票。此代码将字段 receiverId 添加到发票模型。
现在我想获取包括收款人在内的发票清单。我该怎么做?
Invoice.find({include: "reciever"})
或者
Invoice.find({include: "user"})
没用,返回:"Relation \"receiver\" is not defined for Invoice model" 错误。
感谢您的帮助。
您必须在发票模型中定义 belongsTo 关系。
invoice.json:
{//...
"relations":{
"receiver": {
"type": "belongsTo",
"model": "Receiver"
},
}
//...
}
然后你可以这样查询你的模型:
Invoice.find({include: "receiver"}, function(data){
console.log(data);
});