Я использую [BookshelfJS] [bookshelfjs] для своего ORM, и мне интересно, как получить доступ к данным в таблице though
.Как получить доступ к данным в таблице `through` с книжной полкой
У меня есть 3 модели, Recipe
, Ingredient
и RecipeIngredient
, который соединяет два.
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');
}
}));
Я затем попытаться извлечь Recipe
вместе со всеми Ingredients
, однако, не может работать, как получить доступ measurement
на RecipeIngredient
.
Recipe
.forge({
id: 1
})
.fetch({
withRelated: ['ingredients']
})
.then(function (model) {
console.log(model.toJSON());
})
.catch(function (err) {
console.error(err);
});
Возврат:
{
"id": 1,
"name": "Delicious Recipe",
"ingredients": [
{
"id": 1,
"name": "Tasty foodstuff",
"_pivot_id": 1,
"_pivot_recipe_id": 1,
"_pivot_ingredient_id": 1
}
]
}
При отсутствии measurement
значения.
Я думал, что метод .withPivot(['measurement'])
мог бы захватить значение, но он не возвращает никаких дополнительных данных.
Я что-то пропустил или неправильно понял, как это работает?
Спасибо. Я удалил '.through', и теперь он работает. – rockingskier