Best way to mergetwo or more objects + child arrays and then save

Hey all!

Sorry in advance if the below is a bit messy in trying to explain what I want to do but thought I would put it out there.

I have a little question but unsure if there is an easier way in ColdBox/ORM to do this. I would like to merge two objects child arrays into the first object. For example see basic handler function below:

`
function basicMerge(event,rc,prc) {
prc.obj1 = userService.get(rc.id1);
prc.obj2 = userService.get(rc.id2);
}

`

Example return data for obj1:

  • FirstName (Bob)
  • LastName (Bobby)
  • Hobbies (Array of hobbies)
    — Cars
    — Sports
    — Movies

Example return data for obj2:

  • FirstName (B o b)
  • LastName (B o b b y)
  • Hobbies (Array of hobbies)
    — Music
    — TV
    — Photography

Of course the above is very simple, it’s just getting 2 objects based on different ID but I would like to merge/copy all the child arrays from obj2 into obj1 so the end result would look like this:

Example data for obj1 after merge:

  • FirstName (Bob)
  • LastName (Bobby)
  • Hobbies (Array of hobbies)
    — Cars
    — Sports
    — Movies
    — Music
    — TV
    — Photography

Anyone know the easiest way to do this? Just to reconfirm the above is pretty simple and I know I could do some sort of ArrayAppend(), update ID of hobby and then save but I need to update ID relations on a lot of data spread over many models.

Cheers! :slight_smile:

What type of relationship is Hobbies on User?

It could be a ‘many to one’ or ‘many to many’ or a ‘one to many’ - Really I need to be able to change where the UserId is ‘1’ I need it moved to ‘2’ on all/any relationship properties.

Need it to be pretty dynamic which is the issue as I am looking over the Model property meta data to list out the properties to then see relation arrays which need to be moved over to the new object (i.e.Obj 1).

Cheers!

I guess it depends what you’re after. For example, you say merge/copy, but which is it? Should obj2 copy its child properties into obj1 so that obj1 gains new child objects and obj2 stays the same…or should the copied objects from obj2 be removed from obj2 once the merge is completed?