【问题标题】:sails.js extra fields on many to many relationships join table多对多关系连接表上的sails.js 额外字段
【发布时间】:2015-06-26 09:18:17
【问题描述】:

我有两个模型:

Order.js

module.exports = {
    attributes: {
        //Id is generated by mongo and is a primary key
        address: {
            type: 'string',
            required: true,
            size: 1000
        },
        status: {
            type: 'string',
            required: true,
            size: 15,
            defaultsTo: 'pending',
            enum: ['pending', 'processing', 'cancelled']
        },
        total: {
            type: 'string',
            required: true,
            size: 50
        },
        menues: {
            collection: 'menu',
            via: 'orders',
            dominant: true
        },
        customer: {
            model: 'customer'
        }
    }
};

Menu.js

module.exports = {
    attributes: {
        //Id is generated by mongo
        name: {
            type: 'string',
            required: true,
            size: 255
        },
        description: {
            type: 'string',
            required: true,
            size: 1000
        },
        price: {
            type: 'string',
            required: true,
            size: 15
        },
        picture: {
            type: 'string',
            size: 255
        },
        //Relations
        orders: {
            collection: 'order',
            via: 'menues'
        }
    }
};

有一个多对多的关系,它完美地工作并创建一个 menu_orders__order_menues 集合,一切都很好。

但我需要按顺序保存每个菜单的数量。就像菜单 id "1" 数量是 2 等等。

所以一个额外的数据是数量:每个订单应该有每个菜单的确定数量

最好的方法是什么,以及如何将它存储在数据库中? 我想以某种方式将它存储在关系表中,但我不知道如何。

请告诉我最正确和可接受的解决方案。

附:我目前使用 sails-mongo,但很快会迁移到 MySQLPostgres,所以我需要以相对的方式完成。

【问题讨论】:

  • beforeCreate函数呢?
  • 感谢 Leestex!请给我更多的线索,拜托!

标签: mysql sql node.js sails.js waterline


【解决方案1】:

quantity 字段添加到您的菜单模型架构中,并且可以使用afterCreate 函数为其设置值。

Order.js

module.exports = {
    attributes: {
        address: {
            type: 'string',
            required: true,
            size: 1000
        },
        status: {
            type: 'string',
            required: true,
            size: 15,
            defaultsTo: 'pending',
            enum: ['pending', 'processing', 'cancelled']
        },
        total: {
            type: 'string',
            required: true,
            size: 50
        },
        menues: {
            collection: 'menu',
            via: 'orders',
            dominant: true
        },
        customer: {
            model: 'customer'
        }
    },
    afterCreate: function (order, cb) {
        Menu.find(order.menues, function (err, menues) {
            if (err) return cb(err);
            async.each(menues, function (menu, next) {
                menu.quantity++;
                menu.save(next);
            }, cb);
        });
    }
};

菜单.js

module.exports = {
    attributes: {
        name: {
            type: 'string',
            required: true,
            size: 255
        },
        description: {
            type: 'string',
            required: true,
            size: 1000
        },
        price: {
            type: 'string',
            required: true,
            size: 15
        },
        picture: {
            type: 'string',
            size: 255
        },
        quantity: {
            type: 'number',
            defaultsTo: 0
        },
        orders: {
            collection: 'order',
            via: 'menues'
        }
    }
};

这不是一个完整的解决方案。您还必须处理 afterUpdateafterDestroy 函数。

【讨论】:

  • 哦,我明白了,谢谢你,Leestex!但这将是一些菜单,而不是我想的每个订单。我需要每个订单,就像您订购 5 个大汉堡菜单和 1 个糖尿病患者一样。但是我会阅读之前/之后(更新)方法,也许我会想出一些东西......
  • 嗨,Alex 想知道您是否找到了解决方案?我遇到了同样的障碍。
  • 嘿@eptgrant 进展发生在这里:github.com/balderdashy/waterline/issues/705 并且非常缓慢。所以我有点覆盖注入连接表结果的默认查询......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2011-03-23
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
相关资源
最近更新 更多