Merge entries in CakePHP
Posted
by Andrea
on Stack Overflow
See other posts from Stack Overflow
or by Andrea
Published on 2010-04-28T15:51:12Z
Indexed on
2010/04/28
15:53 UTC
Read the original article
Hit count: 452
Let's say I have a Model, for example User, and I want to merge two instances of this Model, say merge User2 into User1. Explicitly this is what I mean:
If a field is already filled in User1, it should remain the same
If a field is missing in User1 but is present in User2, it should be copied
If SomeModel BelongsTo User, every instance of SomeModel pointing to User2 should be modified to point to User1
Same if SomeModel HasAndBelongsToMany User
If SomeModel HasMany User, and SomeModel1 Has User2 but no other instance Has User1, it should be modified so that SomeModel1 has User1 instead
If SomeModel HasMany User, SomeModel1 Has User1 and SomeModel2 Has User2... well, I'm not sure here, I guess the only solution is to discard SomeModel2, since User1 can BelongTo only one SomeModel.
Finally User2 should be removed.
Is there a way to automate this? Maybe a Behaviour? If not, I may consider creating it, since I will need it a lot.
© Stack Overflow or respective owner