LEFT / RIGHT连接node-orm2

时间:2015-10-10 21:34:03

标签: mysql node.js join node-orm2

我在我的节点开发中使用dresende / node-orm2 ORM。但是我需要做一些LEFT / RIGHT连接,但我找不到如何在文档中。在尝试进行多个连接时我也遇到了问题。

此代码:

crModel.hasOne ('client', cliModel, {

    field:  'client_id'
});

crModel.hasOne ('office', boModel, {

    field:  'bo_id'
});

crModel.findByClient ({}).findByOffice ({}).find ({

    client_id:  1,
    bo_id:      1
}, function () {

    console.log (arguments);
});

生成此查询:

SELECT `t1`.`cr_id`, `t1`.`cr_datetime`, `t1`.`credit_id`, `t1`.`gs_id`, `t1`.`cellphone_id`, `t1`.`bo_id`, `t1`.`client_id` FROM `CreditRequests` `t1` JOIN `BranchOffices` `t2` ON `t2`.`bo_id` = `t1`.`bo_id` WHERE `t1`.`client_id` = 1 AND `t1`.`bo_id` = 1

谢谢,抱歉我的英语很差。

1 个答案:

答案 0 :(得分:0)

ORM的诀窍在于设想关系,而不是让它工作的命令。在ORM2中有一个hasMany关联 - 我会从那里开始。来自文档:

patient.getDoctors(function..)           // List of doctors
patient.addDoctors(docs, function...)    // Adds entries to join table
patient.setDoctors(docs, function...)    // Removes existing entries in join table, adds new ones
patient.hasDoctors(docs, function...)    // Checks if patient is associated to specified doctors
patient.removeDoctors(docs, function...) // Removes specified doctors from join table

doctor.getPatients(function..)
etc...

// You can also do:
patient.doctors = [doc1, doc2];
patient.save(...)

https://github.com/dresende/node-orm2