From 7b8001a847e9a89dc7203c5cf2ead715b9bcd45b Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 25 Oct 2017 10:15:25 +0200 Subject: [FIX] hr_expense,web: explicit link_to command for o2m The ORM doesn't automatically link subrecords when it receives an update command for a one2many. However, it may be necessary when the default_get returns existing records for a one2many field. It only worked before this rev. for the hr_expense model because of an hack from 2016 (to reproduce: go to Expenses, check some expenses from the list view, in Actions, click on 'Submit to Manager', the new hr_expense_sheet contains the checked expenses (one2many), and the webclient only sent an update command by linked subrecord). This rev. fixes the problem properly: when the default_get returns existing subrecords for a one2many field, a link_to command (4) is generated alongside the update (1) command. --- addons/web/static/src/js/views/basic/basic_model.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src/js/views/basic/basic_model.js') diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index c450e2bedc7..1d8e68c3e62 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -2634,13 +2634,21 @@ var BasicModel = AbstractModel.extend({ } commands[fieldName].push(command); } else if (_.contains(addedIds, list.res_ids[i])) { - // this is a new id + // this is a new id (maybe existing in DB, but new in JS) relRecord = _.findWhere(relRecordAdded, {res_id: list.res_ids[i]}); changes = this._generateChanges(relRecord, options); if ('id' in changes) { + // the subrecord already exists in db delete changes.id; - commands[fieldName].push(x2ManyCommands.update(relRecord.res_id, changes)); + if (this.isNew(record.id)) { + // if the main record is new, link the subrecord to it + commands[fieldName].push(x2ManyCommands.link_to(relRecord.res_id)); + } + if (!_.isEmpty(changes)) { + commands[fieldName].push(x2ManyCommands.update(relRecord.res_id, changes)); + } } else { + // the subrecord is new, so create it commands[fieldName].push(x2ManyCommands.create(changes)); } } -- cgit v1.2.3