Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Bookshelf.js中创建与自定义字段名的多对多关系?_Javascript_Node.js_Bookshelf.js - Fatal编程技术网

Javascript 如何在Bookshelf.js中创建与自定义字段名的多对多关系?

Javascript 如何在Bookshelf.js中创建与自定义字段名的多对多关系?,javascript,node.js,bookshelf.js,Javascript,Node.js,Bookshelf.js,我想在Bookshelf.js中创建一个多对多关系,并自己指定FK列的名称。我还希望能够访问Bookshelf中的helper表,如下所示: var Doctor = bookshelf.Model.extend({ patients: function() { return this.belongsToMany(Patient).through(Appointment); } }); var Appointment = bookshelf.Model.extend({

我想在Bookshelf.js中创建一个多对多关系,并自己指定FK列的名称。我还希望能够访问Bookshelf中的helper表,如下所示:

var Doctor = bookshelf.Model.extend({

  patients: function() {
    return this.belongsToMany(Patient).through(Appointment);
  }

});

var Appointment = bookshelf.Model.extend({

  patient: function() {
    return this.belongsTo(Patient);
  },

  doctor: function() {
    return this.belongsTo(Doctor);
  }

});

var Patient = bookshelf.Model.extend({

  doctors: function() {
    return this.belongsToMany(Doctor).through(Appointment);
  }

});

我该怎么做呢?

考虑到上面的例子,我们可以这样做:

var Doctor = bookshelf.Model.extend({

  tableName: "d_doctor",
  idAttribute: "d_doctorid",

  patients: function() {
    return this.belongsToMany(Patient).through(Appointment,"a_d_doctorid","a_p_patientid");
  }

});

var Appointment = bookshelf.Model.extend({

  tableName : "a_appointment",
  idAttribute : "a_appointmentid",

  patient: function() {
    return this.belongsTo(Patient,"a_p_patientid");
  },

  doctor: function() {
    return this.belongsTo(Doctor,"a_d_doctorid");
  }

});

var Patient = bookshelf.Model.extend({

  tableName : "p_patient",
  idAttribute : "p_patientid",

  doctors: function() {
    return this.belongsToMany(Doctor).through(Appointment,"a_p_patientid", "a_d_doctorid");
  }

});