Best way to save a ordered List to the Database while keeping the ordering

前端 未结 12 1093
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 17:27

I was wondering if anyone has a good solution to a problem I\'ve encountered numerous times during the last years.

I have a shopping cart and my customer explicitly

12条回答
  •  春和景丽
    2020-11-30 17:57

    I wouldn't recommend the A, AA, B, BA, BB approach at all. There's a lot of extra processing involved to determine hierarchy and inserting entries in between is not fun at all.

    Just add an OrderField, integer. Don't use gaps, because then you have to either work with a non-standard 'step' on your next middle insert, or you will have to resynchronize your list first, then add a new entry.

    Having 0...N is easy to reorder, and if you can use Array methods or List methods outside of SQL to re-order the collection as a whole, then update each entry, or you can figure out where you are inserting into, and +1 or -1 each entry after or before it accordingly.

    Once you have a little library written for it, it'll be a piece of cake.

提交回复
热议问题