How to access data on a `through` table with Bookshelf

半腔热情 提交于 2019-12-02 21:21:03

问题


I am using [BookshelfJS][bookshelfjs] for my ORM and am wondering how to access data on a though table.

I have 3 Models, Recipe, Ingredient and RecipeIngredient which joins the two.

var Recipe = BaseModel.extend({
  tableName: 'recipe',

  defaults: { name: null },

  ingredients: function () {
    return this
      .belongsToMany('Ingredient')
      .through('RecipeIngredient')
      .withPivot(['measurement']);
  }
}));

var Ingredient = BaseModel.extend({
  tableName: 'ingredients',

  defaults: { name: null },

  recipes: function () {
    return this
      .belongsToMany('Recipe')
      .through('RecipeIngredient');
  }
}));

var RecipeIngredient = BaseModel.extend({
  tableName: 'recipe_ingredients',

  defaults: { measurement: null },

  recipe: function () {
    return this.belongsToMany('Recipe');
  },

  ingredient: function () {
    return this.belongsToMany('Ingredient');
  }
}));

I then attempt to retrieve a Recipe along with all the Ingredients however cannot work out how to access measurement on the RecipeIngredient.

Recipe
  .forge({
    id: 1
  })
  .fetch({
    withRelated: ['ingredients']
  })
  .then(function (model) {
    console.log(model.toJSON());
  })
  .catch(function (err) {
    console.error(err);
  });

Return:

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

With no measurement value.

I had thought that the .withPivot(['measurement']) method would have grabbed the value but it does not return any additional data.

Have I missed something or misunderstood how this works?


回答1:


I'm not exactly sure why you want to use through. If it's just a basic many-to-many mapping, you can achieve this by doing the following:

var Recipe = BaseModel.extend({
  tableName: 'recipe',

  defaults: { name: null },

  ingredients: function () {
    return this
      .belongsToMany('Ingredient').withPivot(['measurement']);
  }
}));

var Ingredient = BaseModel.extend({
  tableName: 'ingredients',

  defaults: { name: null },

  recipes: function () {
    return this
      .belongsToMany('Recipe').withPivot(['measurement']);;
  }
}));

You don't need an additional model for junction table. Just be sure to define a junction table in your database as ingredients_recipe (alphabetically joining the name of tables!). Or , you can provide your own custom name to belongsToMany function for what the junction table should be named. Be sure to have ingredients_id and recipe_id in ingredients_recipe

This is pretty much it. Then you can do

Recipe
  .forge({
    id: 1
  })
  .fetch({
    withRelated: ['ingredients']
  })
  .then(function (model) {
    console.log(model.toJSON());
  })
  .catch(function (err) {
    console.error(err);
  });


来源:https://stackoverflow.com/questions/30411367/how-to-access-data-on-a-through-table-with-bookshelf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!