Skip to content

Commit b04b66f

Browse files
author
Antoine Huret
committed
fill Relation ids when it's set by the user. Potential break BC
1 parent 4e76e2d commit b04b66f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,54 @@ func (r *Relation) UpdateRecord(record int64, values interface{}) {
165165
// https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write
166166
func (r *Relation) DeleteRecord(record int64) {
167167
r.v = append(r.v, newTuple(2, record, 0))
168+
r.ids = []int64{}
168169
}
169170

170171
// RemoveRecord is an helper to remove an existing record of one2many or many2many.
171172
// https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write
172173
func (r *Relation) RemoveRecord(record int64) {
173174
r.v = append(r.v, newTuple(3, record, 0))
175+
r.ids = []int64{}
174176
}
175177

176178
// AddRecord is an helper to add an existing record of one2many or many2many.
177179
// https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write
178180
func (r *Relation) AddRecord(record int64) {
179181
r.v = append(r.v, newTuple(4, record, 0))
182+
r.ids = addRecord(r.ids, record)
180183
}
181184

182185
// RemoveAllRecords is an helper to remove all records of one2many or many2many.
183186
// https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write
184187
func (r *Relation) RemoveAllRecords() {
185188
r.v = append(r.v, newTuple(5, 0, 0))
189+
r.ids = []int64{}
186190
}
187191

188192
// ReplaceAllRecords is an helper to replace all records of one2many or many2many.
189193
// https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write
190194
func (r *Relation) ReplaceAllRecords(newRecords []int64) {
191195
r.v = append(r.v, newTuple(6, 0, newRecords))
196+
r.ids = newRecords
197+
}
198+
199+
func addRecord(records []int64, record int64) []int64 {
200+
for _, r := range records {
201+
if r == record {
202+
return records
203+
}
204+
}
205+
return append(records, record)
206+
}
207+
208+
func removeRecord(records []int64, record int64) []int64 {
209+
newRecords := make([]int64, len(records)-1)
210+
for idx, r := range records {
211+
if r == record {
212+
copy(newRecords, records[0:idx])
213+
copy(newRecords[idx:], records[idx+1:])
214+
return newRecords
215+
}
216+
}
217+
return records
192218
}

0 commit comments

Comments
 (0)