Como acessar dados em uma tabela `through 'com Bookshelf
Estou usando [BookshelfJS] [bookshelfjs] para o meu ORM e estou pensando em como acessar dados em umthough
mesa.
Tenho 3 modelos,Recipe
, Ingredient
eRecipeIngredient
que une os dois.
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');
}
}));
Eu tento recuperar umRecipe
junto com todoIngredients
no entanto, não pode descobrir como acessarmeasurement
noRecipeIngredient
.
Recipe
.forge({
id: 1
})
.fetch({
withRelated: ['ingredients']
})
.then(function (model) {
console.log(model.toJSON());
})
.catch(function (err) {
console.error(err);
});
Retorna:
{
"id": 1,
"name": "Delicious Recipe",
"ingredients": [
{
"id": 1,
"name": "Tasty foodstuff",
"_pivot_id": 1,
"_pivot_recipe_id": 1,
"_pivot_ingredient_id": 1
}
]
}
Com nenhummeasurement
valor.
Eu pensei que o.withPivot(['measurement'])
O método teria capturado o valor, mas não retorna nenhum dado adicional.
Perdi alguma coisa ou entendi mal como isso funciona?