简体   繁体   English

如何访问具有附加一对多关系的联接表上的数据

[英]How to access data on a join table that has additional one to many relations

Hi I have almost the same issue as this previous asked question How to access data on a through table with Bookshelf but instead of a value measurement i hava key to table with unit ( L, Kg, mg, ml and so one ) that is attached to the RecipeIngredient table. 嗨,我有一个与上一个问题几乎相同的问题, 如何使用书架访问through表上的数据,而不是值测量,我使用了附有单位(L,Kg,mg,ml等)的表的键到RecipeIngredient表。 what i want to have is similar but with my unit? 我想要拥有的东西与我的单位相似? like this: 像这样:

{
  "id": 1,
  "name": "Delicious Recipe",
  "ingredients": [
    {
      "id": 1,
      "name": "Tasty foodstuff",
      "_pivot_id": 1,
      "_pivot_recipe_id": 1,
      "_pivot_ingredient_id": 1,
      "_pivot_amount": 10,
      "Unit": "kg"
    }]
}

My many to many table looks like this: 我的多对多表如下所示:

CREATE TABLE IF NOT EXISTS `Ingredients_Recipe` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Recipe_id` int(11) NOT NULL,
  `Ingredient_id` int(11) NOT NULL,
  `amount` int(11) NOT NULL,
  `Unit_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
KEY `Ingredient_id` (`Ingredient_id`),
KEY `Unit_id` (`Unit_id`),
KEY `Recipe_Ingredients_ibfk_1` (`Recipe_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

And my Unit table looks like this: 我的单位表如下所示:

CREATE TABLE IF NOT EXISTS `Units` (
  `id` int(11) NOT NULL,
  `unit` varchar(10) NOT NULL,
UNIQUE KEY `unit` (`unit`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

My Recipe table looks like this: 我的食谱表如下所示:

CREATE TABLE IF NOT EXISTS `Recipe` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'identifier',
  `name` varchar(254) NOT NULL COMMENT 'Name of the recipe',
  `description` text NOT NULL COMMENT 'Recipe description',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

and my Ingredient table looks like this: 我的成分表如下所示:

CREATE TABLE IF NOT EXISTS `Ingredients` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(254) NOT NULL,
PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5;

My Recipe model looks like this 我的食谱模型看起来像这样

module.exports = Bookshelf.Model.extend({
  tableName: 'Recipe',

  ingredients: function() {
    var Ingredient = require("./Ingredient");

    return this.belongsToMany(Ingredient);
  }
});

and my get request looks like this: 我的获取请求看起来像这样:

 Recipe.forge({id: "1"})
    .fetch({withRelated: ["ingredients"]})
    .then( function (recipe) {
      resp.json({error: false, data: recipe.toJSON()});
  })
    .otherwise(function (err) {
    resp.status(500).json({error: true, data: {message: err.message}});
  });

If you run this i will get a json response the looks like this: { "id": 1, "name": "Delicious Recipe", "ingredients": [ { "id": 1, "name": "Tasty foodstuff", "_pivot_id": 1, "_pivot_recipe_id": 1, "_pivot_ingredient_id": 1, }] } 如果运行此命令,我将得到一个json响应,如下所示:{“ id”:1,“ name”:“美味食谱”,“成分”:[{“ id”:1,“ name”:“美味食品“,” _pivot_id“:1,” _pivot_recipe_id“:1,” _pivot_ingredient_id“:1,}]}

but i want to add 但我想补充

"_pivot_amount": 10,
"Unit": "kg"

in the ingredients object. 在配料对象中。

how do i do that? 我怎么做?

Regards 问候

What i try to do is a cookbook app. 我试图做的是一个菜谱应用程序。 I am using node.js as restAPI server and when i sending a GET request, what i want is to get recipe with ingredients and amount of the ingredient and with what unit the amount is in. 我正在使用node.js作为restAPI服务器,当我发送GET请求时,我想要的是获取包含配料和配料量以及配料单位的配方。

我使用了一个名为bookends https://city41.github.io/bookends/的小图书馆,基本上可以快速完成所需的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM