如何使用环回更新相关模型

时间:2015-12-15 13:19:30

标签: has-many relation loopback

我有相关的模型,问题可以有很多答案:

这是我的问题.json:

{
  "name": "Question",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "metal-quizz",
    "table": "Question"
  },
  "properties": {
    "id": {
      "type": "Number",
      "id": true,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "label": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "label",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "level": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "level",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {
    "answers": {
      "type": "hasMany",
      "model": "Answer",
      "foreignKey": "questionId"
    }
  },
  "acls": [],
  "methods": {}
}

这是我的回答.json:

{
  "name": "Answer",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "metal-quizz",
    "table": "Answer"
  },
  "properties": {
    "id": {
      "type": "Number",
      "id": true,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "questionId": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "questionId",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "label": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "label",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "status": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 3,
      "scale": 0,
      "mysql": {
        "columnName": "status",
        "dataType": "tinyint",
        "dataLength": null,
        "dataPrecision": 3,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {
    "question": {
      "type": "belongsTo",
      "model": "Question",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

我希望有一个网页,其中包含一个显示问题及其所有相关答案的表单,并允许我同时更新所有问题。 我管理显示部分没有任何问题,但我对更新部分感到疯狂。

理论上它是可能的,因为我可以通过以下路线通过探险家来做到这一点:

PUT /Questions/{id} to update question
PUT /Questions/{id}/answers/{fk} to update answers individually

所以我创建了一个远程方法,但是我无法理解我可以使用哪种方法来逐一更新它们:

Question.putWithDeps = function(question, cb) {
    // Update Question 
    Question.upsert(question, function(err, model) {
        // What code am I supposed to put here to update question.answers ???
    }
};
Question.remoteMethod(
    'putWithDeps',
    {
        http: {path: '/withDeps', verb: 'put'},
        accepts: {arg: 'instance', type: 'object'},
        returns: {root: true, type: 'object' } 
    }
);

我弄错了吗? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

它看起来像这样(不是确切的代码,只是粗略的草图而未经过测试,精确的args可能需要调整)。还需要修改您的远程方法以传入新的答案数据:

Question.putWithDeps = function(question, answers, cb) {
  // Update Question 
  Question.upsert(question, function(err, question) {

    // get all answers that belong to the parent question
    question.answers({}, function(err, answers) {

      // answers = array of answers that belong to the Question
      // example with index 0:
      answer[0].upsert({answer object}, function(err, answer) {
        // check for err or success
      })

      // or loop on the array and update inside the loop (or promisify using .then())

    });

    // could also destroy all answers and create all new ones
    question.answers.destroyAll(function(err) {

      question.answers.create(<array of new answers>,function(err) {
        ...
      });
    });

  }
};

查看hasMany文档,底部显示添加到模型中的方法:

https://docs.strongloop.com/display/public/LB/HasMany+relations