Page 1 of 1

Handling modification of a list of data relations

Posted: Sun Feb 19, 2012 6:30 am
by jraede
This is a sort of general question that applies to several areas of my application development.

Say I have an object that is related to a bunch of other objects, and the relations are held in a MySQL table called "relations", with columns for "first_object", "second_object", and, let's say "relation_order". The user can go in and modify the first object, and there can edit the list of relations it has with other objects by removing and re-ordering list items. When the user has modified the list, I have two options. One, I can just delete all relations that this object has with any second object, and then create them all from scratch. Two, I can go through all existing relations and all new relations, find the ones to delete and create with array_diff and array_intersect, and go from there. I have done this both ways in various parts of my applications and haven't noticed any real difference in speed, but it could potentially be noticeable if their databases grow substantially.

Is there a clear benefit to either one of these methods? Or maybe it depends on the situation? I'd like to hear thoughts.

Thanks