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.
We made a Merge behaviour exactly for this purpose back in 2008.
It may require some tweaking to work correctly with the latest CakePHP stable release but you can find it here
精彩评论